Null pointer exception when creating content instance?

In my Android, I read contacts using content resolver. But the content resolver is not initialized; it gives a NullPointerException . I want to use string arrays in another class and get the values ​​in them, I have to query the database and read contacts from the phone, but stick to this exception, as indicated in the code:

 //this code is in some other class... et= (EditText) findViewById(R.id.searchcontact); et.addTextChangedListener(new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub lva.getFilter().filter(s); RefreshListView rlv=new RefreshListView();<----class is getting called from here lva.notifyDataSetInvalidated(); lva = new ListViewAdapterContacts(AllContactsActivity.this, rlv.names, rlv.types, rlv.phones, rlv.ids); lv.setAdapter(lva); } public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } public void afterTextChanged(Editable s) { // TODO Auto-generated method stub // lva.notifyDataSetChanged(); } }); ...............********......... public class RefreshListView extends ListActivity{ String[] names,phones,types,ids; String name,id,phoneNumber; int type; ArrayList <String> newValues; public void onCreate(Bundle b){ super.onCreate(b); newValues=SetNewList.getNames(); getContacts(); } public void getContacts(){ int foo = 0; ContentResolver cr = getContentResolver();<---//null pointer exception Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC"); if (cur.getCount() > 0) { names = new String[cur.getCount()]; phones = new String[cur.getCount()]; types = new String[cur.getCount()]; ids = new String[cur.getCount()]; while (cur.moveToNext()) { name = cur .getString(cur .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); for(int i=0;i<=newValues.size();i++){ if(newValues.get(i).equalsIgnoreCase(name)) { id = cur.getString(cur .getColumnIndex(ContactsContract.Contacts._ID)); String hasPhone = cur .getString(cur .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); if (Integer.parseInt(hasPhone) == 1) { Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null); while (phones.moveToNext()) { phoneNumber = phones .getString(phones .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); type = phones.getInt(phones.getColumnIndex(Phone.TYPE)); } // while phones.close(); }// if else { phoneNumber = "unknown"; type = 0; }// else phones[foo] = phoneNumber; names[foo] = name; ids[foo] = id; if (type == 0) { String value = "unknown"; types[foo] = value; } else if (type == 1) { String value = "home"; types[foo] = value; }// if else if (type == 2) { String value = "mobile"; types[foo] = value; }// else if else if (type == 3) { String value = "Work"; types[foo] = value; } else if (type == 4) { String value = "Workfax"; types[foo] = value; } else if (type == 5) { String value = "Home Fax"; types[foo] = value; } else if (type == 6) { String value = "Pager"; types[foo] = value; } else { String value = "Other"; types[foo] = value; } foo++; } } }// while }// if }// get contacts } 
+4
source share
2 answers

Do not do that...

 public class RefreshListView extends ListActivity{ public RefreshListView() { ... } } 

Do not use the constructor for Android Activity !!!

You must remove this constructor and put the code in the Activity onCreate(...) method.

See the documentation for Activity , as well as read Activities .

Try something like this ...

 public class RefreshListView extends ListActivity{ String[] names,phones,types,ids; String name,id,phoneNumber; int type; ArrayList <String> newValues; ContentResolver cr = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cr = getContentResolver(); newValues=SetNewList.getNames();//getnames is of static type getContacts(); } public void getContacts() { int foo = 0; // REMOVE THE NEXT LINE !!! // ContentResolver cr = getContentResolver(); ... } } 
+2
source

getContentResolver () should be called inside the Activity implementation - try overriding onCreate (). This is clearer if you call it this.getContentResolver() in your activity = easier to read.

Check this sample code (after the "Data Binding" section).

You can use ContentResolver in every other component of the application, you just need Context . In the case of an Activity, you automatically use an Activity context. You can also use the Application Context - they mainly differ in their life cycles.

+1
source

Source: https://habr.com/ru/post/1384896/


All Articles