以编程方式填充listview

时间:2012-03-05 15:08:58

标签: android listview android-arrayadapter

我正在尝试在列表视图中显示存储在模拟器上的联系人。稍后虽然它只是应用程序存储的一组选定联系人(需要有关此存储和检索的建议)。由于列表不能是硬编码的字符串数组,我试图以编程方式填充listview但是我收到错误。

这里是代码PS:初学者android dev。

我在清单Reads_contacts中包含了必要的权限..所以这不是问题

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);
}

日志猫必须这样说:我不确定是什么表明我正在粘贴整个东西

    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

1 个答案:

答案 0 :(得分:0)

您的代码中存在许多错误。 NPE只是第一个。

//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;
        }
    });
}