I’m getting a NullPointerException in my Android app and, as far as I can tell from LogCat, it’s happening when trying to set a listview to clickable. This is my code.
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);
}
});
}
}
This is the LogCat output
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
The code on line 31 of MyActivity that LogCat says caused the exception is
lstView.setClickable(true);
FIXED
Here is the working code. There are a few changes.
public class MyActivity extends ListActivity {
private SQLiteDatabase database;
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);
}
});
}
}
you should get the listView reference from the layout. probably use something like this..
and then set listView to clickable.
on the other hand, why do you want to set a list view to be clickable?, you need not do it, list items are clickable by default.