getting null pointer exception?

前端 未结 5 1403
别那么骄傲
别那么骄傲 2020-12-22 11:11

I am trying to have name of contacts in one array and their types in another array,but can\'t get through with null pointer exception.here is my code.I have pointed out the

5条回答
  •  猫巷女王i
    2020-12-22 11:33

    You are getting null pointer because you are accessing wrong index.

    try this:

    s=new String[cur.getCount()];
    int  i=1;
    while (cur.moveToNext()) {    
    
                       String id = cur.getString(indexID);    
                       name[i-1] = cur.getString(indexName);  
                       phoneType[i-1] =  cur.getString(indexPhoneType);       
    
    
                      String temp="id="+id+"-name="+name[i-1]+"-phoneType="+phoneType[i-1];
                      s[i-1]=temp;
                      i++;
        }
    

    and also lv=(ListView)findViewById(R.id.listview); instead of lv=(ListView)findViewById(android.R.id.list);

    You should add a condition here:

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
           testGetContacts();
    
    if(s.length()>0)
    {
        lv = (ListView)findViewById(R.id.listview);
        ArrayAdapter sa=new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1,s);
        lv.setAdapter(sa);
    }
    else
    {
       Toast.makeText(getApplicationContext(),"Nothing Found",Toast.LENGTH_SHORT).show();
    }
    

提交回复
热议问题