尝试将listView设置为可单击的NullPointerException

时间:2012-01-10 20:18:18

标签: android listview nullpointerexception

我的Android应用程序中出现了NullPointerException,据我所知,从LogCat可以看出,在尝试将listview设置为可点击时会发生这种情况。这是我的代码。

public class MyActivity extends Activity {
private ListView lstView = (ListView) findViewById(R.id.listView1);
private SQLiteDatabase database;

private static final String fields[] = { "field1", "field2", "field3" };


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    CursorAdapter dataSource;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //lstView.setClickable(true);   
private static final String fields[] = { "field1", "field2", "field3" };


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    CursorAdapter dataSource;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lstView.setClickable(true);

    DatabaseHelper helper = new DatabaseHelper(this);
    database = helper.getWritableDatabase();
    Cursor data = database.query("mydb", fields, null, null, null, null, null);
    dataSource = new SimpleCursorAdapter(this, R.layout.row, data, fields, new int[] {R.id.field1, R.id.field2});
    database.close();
    lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            Intent newActivity = new Intent(getApplicationContext(), NewIntent.class);
            newActivity.putExtra("itemId",lstView.getItemIdAtPosition(position));
            // start activity
            startActivity(newActivity);
          }
        });

}


}

这是LogCat输出

01-10 14:03:53.740: E/AndroidRuntime(30361): FATAL EXCEPTION: main
01-10 14:03:53.740: E/AndroidRuntime(30361): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.jonward.myapp/app.jonward.myapp.MyActivity}: java.lang.NullPointerException
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.app.ActivityThread.access$1500(ActivityThread.java:132)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.os.Looper.loop(Looper.java:150)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.app.ActivityThread.main(ActivityThread.java:4263)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at java.lang.reflect.Method.invokeNative(Native Method)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at java.lang.reflect.Method.invoke(Method.java:507)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at dalvik.system.NativeStart.main(Native Method)
01-10 14:03:53.740: E/AndroidRuntime(30361): Caused by: java.lang.NullPointerException
01-10 14:03:53.740: E/AndroidRuntime(30361):    at  app.jonward.castr.CastrActivity.onCreate(MyActivity.java:31)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
01-10 14:03:53.740: E/AndroidRuntime(30361):    ... 11 more

LogCat所说的导致异常的MyActivity第31行的代码是

lstView.setClickable(true);

固定 这是工作代码。有一些变化。     公共类MyActivity扩展ListActivity {     私有SQLiteDatabase数据库;     private static final String fields [] = {“field1”,“field2”,“_ id”};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    CursorAdapter dataSource;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    DatabaseHelper helper = new DatabaseHelper(this);
    database = helper.getWritableDatabase();
    Cursor data = database.query("castr", fields, null, null, null, null, null);
    dataSource = new SimpleCursorAdapter(this, R.layout.row, data, fields, new int[] {R.id.field1, R.id.field2});
    final ListView view = getListView();

    view.setHeaderDividersEnabled(true);
    view.addHeaderView(getLayoutInflater().inflate(R.layout.row, null));

    database.close();

    view.setOnItemClickListener(new AdapterView.OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

            // Prepare intent
            Intent newActivity = new Intent(getApplicationContext(), NewActivity.class);
            newActivity.putExtra("itemId",view.getItemIdAtPosition(position));
            // start activity
            startActivity(newActivity);
          }
        });

}
}

2 个答案:

答案 0 :(得分:2)

你应该从布局中获取listView引用。可能会使用这样的东西..

listView = (ListView)findViewById(R.id.nameofyourlistView);

然后将listView设置为可点击。

另一方面,为什么要将列表视图设置为可点击?,您不需要这样做,默认情况下列表项是可点击的。

答案 1 :(得分:0)

您需要在使用之前初始化ListView。

lstView= (ListView) findViewById(R.id.your_listview_id);