Reputation: 143
I am trying to display the contacts stored on the emulator in a listview. Later on though it will be just a set of selected contacts stored by the app (need a suggestion for this storage and retrieval). Since the list cannot be a hard coded string array i have tried to fill the listview programmatically but i am getting an error.
here's the code PS: beginner android dev.
I have included the necessary permissions in manifest Reads_contacts.. so that's not a problem
public class settings_manager extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
TabHost th = (TabHost)findViewById(R.id.tabhost);
th.setup();
ContentResolver cr = getContentResolver();
final Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
TabSpec spec1 = th.newTabSpec("tag1");
spec1.setIndicator("CONTACTS");
if(!cur.moveToNext()){
spec1.setContent(R.id.tab1);
}
else{
spec1.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String arg0) {
ListView contacts_list = new ListView(settings_manager.this);
String[] c_list = null; int i=0;
while(cur.moveToNext())
{
int nameindex = cur.getColumnIndex(PhoneLookup.DISPLAY_NAME);
c_list[i] = cur.getString(nameindex); i++;
}
ArrayAdapter<String> contacts_adapter = new ArrayAdapter<String>(null,android.R.layout.simple_list_item_1, c_list);
return contacts_list;
}
}) ;
}
th.addTab(spec1);
TabSpec spec2 = th.newTabSpec("tag2");
spec2.setContent(R.id.tab2);
spec2.setIndicator("tab2");
th.addTab(spec2);
TabSpec spec3 = th.newTabSpec("tag3");
spec3.setContent(R.id.tab3);
spec3.setIndicator("tab3");
th.addTab(spec3);
}
The log cat has to say this :i'm not really sure what indicates what so i'm pasting the entire thing
03-05 22:57:18.185: D/AndroidRuntime(232): Shutting down VM
03-05 22:57:18.185: W/dalvikvm(232): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
03-05 22:57:18.185: E/AndroidRuntime(232): Uncaught handler: thread main exiting due to uncaught exception
03-05 22:57:18.195: E/AndroidRuntime(232): java.lang.RuntimeException: Unable to start activity ComponentInfo{water.tank/water.tank.settings_manager}: java.lang.NullPointerException
03-05 22:57:18.195: E/AndroidRuntime(232): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-05 22:57:18.195: E/AndroidRuntime(232): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-05 22:57:18.195: E/AndroidRuntime(232): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-05 22:57:18.195: E/AndroidRuntime(232): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-05 22:57:18.195: E/AndroidRuntime(232): at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 22:57:18.195: E/AndroidRuntime(232): at android.os.Looper.loop(Looper.java:123)
03-05 22:57:18.195: E/AndroidRuntime(232): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-05 22:57:18.195: E/AndroidRuntime(232): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 22:57:18.195: E/AndroidRuntime(232): at java.lang.reflect.Method.invoke(Method.java:521)
03-05 22:57:18.195: E/AndroidRuntime(232): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-05 22:57:18.195: E/AndroidRuntime(232): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-05 22:57:18.195: E/AndroidRuntime(232): at dalvik.system.NativeStart.main(Native Method)
03-05 22:57:18.195: E/AndroidRuntime(232): Caused by: java.lang.NullPointerException
03-05 22:57:18.195: E/AndroidRuntime(232): at water.tank.settings_manager$1.createTabContent(settings_manager.java:47)
03-05 22:57:18.195: E/AndroidRuntime(232): at android.widget.TabHost$FactoryContentStrategy.getContentView(TabHost.java:617)
03-05 22:57:18.195: E/AndroidRuntime(232): at android.widget.TabHost.setCurrentTab(TabHost.java:320)
03-05 22:57:18.195: E/AndroidRuntime(232): at android.widget.TabHost.addTab(TabHost.java:213)
03-05 22:57:18.195: E/AndroidRuntime(232): at water.tank.settings_manager.onCreate(settings_manager.java:57)
03-05 22:57:18.195: E/AndroidRuntime(232): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-05 22:57:18.195: E/AndroidRuntime(232): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
03-05 22:57:18.195: E/AndroidRuntime(232): ... 11 more
03-05 22:57:18.215: I/dalvikvm(232): threadid=7: reacting to signal 3
03-05 22:57:18.215: E/dalvikvm(232): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
Upvotes: 0
Views: 1684
Reputation: 17139
There were many mistakes in your code. NPE was only the first.
//Prevented cursor moving forward unnecessorily.
if (cur.getCount() == 0) {
spec1.setContent(R.id.tab1);
} else {
spec1.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String arg0) {
ListView contacts_list = new ListView(settings_manager.this);
String[] c_list = new String[cur.getCount()];//NPE was here.
// use cur.getPosition() instead of int i = 0; i++;
while (cur.moveToNext()) {
c_list[cur.getPosition()] = cur.getString(cur
.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
ArrayAdapter<String> contacts_adapter = new ArrayAdapter<String>(
null, android.R.layout.simple_list_item_1, c_list);
//set listadaper
contacts_list.setAdapter(contacts_adapter);
return contacts_list;
}
});
}
Upvotes: 1