使用ArrayAdapter创建列表视图的问题

时间:2012-12-09 01:35:50

标签: android

我在使用我的数据创建ListView时遇到问题。产生它的代码是:

protected void onPostExecute(DtoProjectListItem[] result) {
    mProgressDialog.dismiss();

    // DtoProjectListItem[] Adapter for ListView
    ArrayAdapter<DtoProjectListItem> projects_adapter = new ArrayAdapter<DtoProjectListItem>(
            ProjectListActivity.this,
            R.layout.activity_project_list_item, 
            R.id.tvProjectName, 
            result
    );

    ListView lvProjects = (ListView) findViewById(R.layout.activity_project_list);
    lvProjects.setAdapter(projects_adapter);

}

我得到的错误是:

Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x40cad930)
FATAL EXCEPTION: main
java.lang.NullPointerException
    at org.my.cihub.ProjectListActivity$GetDataAsyncTask.onPostExecute(ProjectListActivity.java:173)
    at org.my.cihub.ProjectListActivity$GetDataAsyncTask.onPostExecute(ProjectListActivity.java:1)
    at android.os.AsyncTask.finish(AsyncTask.java:631)
    at android.os.AsyncTask.access$600(AsyncTask.java:177)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5039)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
12-08 20:31:03.386: I/Process(21412): Sending signal. PID: 21412 SIG: 9

第173行是:

lvProjects.setAdapter(projects_adapter);

有人可以帮我解决这个问题吗?

谢谢! :)

1 个答案:

答案 0 :(得分:0)

问题出在这一行

ListView lvProjects = (ListView) findViewById(R.layout.activity_project_list);

应该是这样的

ListView lvProjects = (ListView) findViewById(R.id.your_list_id);