Note that there are some explanatory texts on larger screens.

plurals
  1. POCouldn't load string value in arraylist
    primarykey
    data
    text
    <p>I want to get the phone contacts and show in the listView. But i couldn't load the string values in array list. i do not know how to solve this.</p> <p>Log cat error:</p> <pre><code>01-07 12:43:49.381: E/AndroidRuntime(863): FATAL EXCEPTION: main 01-07 12:43:49.381: E/AndroidRuntime(863): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rathnasoft.sample/com.rathnasoft.sample.ContactsPhone}: java.lang.NullPointerException 01-07 12:43:49.381: E/AndroidRuntime(863): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 01-07 12:43:49.381: E/AndroidRuntime(863): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 01-07 12:43:49.381: E/AndroidRuntime(863): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 01-07 12:43:49.381: E/AndroidRuntime(863): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 01-07 12:43:49.381: E/AndroidRuntime(863): at android.os.Handler.dispatchMessage(Handler.java:99) 01-07 12:43:49.381: E/AndroidRuntime(863): at android.os.Looper.loop(Looper.java:123) 01-07 12:43:49.381: E/AndroidRuntime(863): at android.app.ActivityThread.main(ActivityThread.java:4627) 01-07 12:43:49.381: E/AndroidRuntime(863): at java.lang.reflect.Method.invokeNative(Native Method) 01-07 12:43:49.381: E/AndroidRuntime(863): at java.lang.reflect.Method.invoke(Method.java:521) 01-07 12:43:49.381: E/AndroidRuntime(863): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 01-07 12:43:49.381: E/AndroidRuntime(863): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-07 12:43:49.381: E/AndroidRuntime(863): at dalvik.system.NativeStart.main(Native Method) 01-07 12:43:49.381: E/AndroidRuntime(863): Caused by: java.lang.NullPointerException 01-07 12:43:49.381: E/AndroidRuntime(863): at com.rathnasoft.sample.ContactsPhone.displayContacts(ContactsPhone.java:67) 01-07 12:43:49.381: E/AndroidRuntime(863): at com.rathnasoft.sample.ContactsPhone.onCreate(ContactsPhone.java:22) 01-07 12:43:49.381: E/AndroidRuntime(863): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 01-07 12:43:49.381: E/AndroidRuntime(863): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 01-07 12:43:49.381: E/AndroidRuntime(863): ... 11 more </code></pre> <p>Coding part:</p> <pre><code>public class ContactsPhone extends Activity { private ListView lvContact; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.contacts); lvContact = (ListView) findViewById(R.id.listView1); ArrayList&lt;String&gt; list = displayContacts(); ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(getBaseContext(), android.R.layout.simple_list_item_1,list); lvContact.setAdapter(adapter); } private ArrayList&lt;String&gt; displayContacts() { ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); ArrayList&lt;String&gt; contact = null; String phoneNo = null; if (cur.getCount() &gt; 0) { while (cur.moveToNext()) { String id = cur.getString(cur .getColumnIndex(ContactsContract.Contacts._ID)); String name = cur .getString(cur .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); if (Integer .parseInt(cur.getString(cur .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) &gt; 0) { Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null); while (pCur.moveToNext()) { phoneNo = pCur .getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); System.out.println("name" + name + "ph no" + phoneNo); Toast.makeText(this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show(); } pCur.close(); } contact.add(name + "\n" + phoneNo); } } return contact; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload