i have a ListActivity to show an Arraylist.
In the onListItemClick() i call another ListActivity.
I have a specific class to extends SQLiteOpenHelper() to manage db.
In the first ListActivity i have this code
protected void onListItemClick(ListView l, View v, int position, long id) {
String selection = l.getItemAtPosition(position).toString();
secondListActivity itemsList= new secondListActivity();
itemsList.showItemsList(selection);
//Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
}
The showComicsList() method in the secondListActivity have
public void showItemsList(String name)
{
DatabasesManager databaseHelper = new DatabasesManager(this);
Cursor series = databaseHelper.getItemsIdByName(name);
Cursor cursor = databaseHelper.getChildren(series.getString(0));
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
//fetching from database and adding to arrayList
results.add(cursor.getString(cursor.getColumnIndex(ChildrensTable.NUMBER)));
}
cursor.close();
//display in screen
this.setListAdapter(
new ArrayAdapter<Object>(
secondListActivity.this,
android.R.layout.simple_list_item_1,
results
)
);
}
the getItemsIdByName() call a cursor
public Cursor getItemsIdByName(String name)
{
return( getReadableDatabase().query(
itemsTable.TABLE_NAME,
null,
"name =" + name,
null,
null,
null,
null,
null));
}
When i call the getItemsIdByName() i get this error
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): FATAL EXCEPTION: main
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): java.lang.NullPointerException
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at zepod.whatelsecomics.databases.wecDatabasesManager.getComics(wecDatabasesManager.java:159)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at zepod.whatelsecomics.WhatelsecomicsListActivity.showComicsList(WhatelsecomicsListActivity.java:25)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at zepod.whatelsecomics.WhatelsecomicsActivity.onListItemClick(WhatelsecomicsActivity.java:47)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at android.widget.ListView.performItemClick(ListView.java:3513)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1800)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at android.os.Handler.handleCallback(Handler.java:587)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at android.os.Handler.dispatchMessage(Handler.java:92)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at android.os.Looper.loop(Looper.java:123)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at android.app.ActivityThread.main(ActivityThread.java:3647)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at java.lang.reflect.Method.invoke(Method.java:507)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-12 14:12:17.906: ERROR/AndroidRuntime(6090): at dalvik.system.NativeStart.main(Native Method)
Can someone help me?
It’s likely the
contexton your secondListActivity is not set since you are just instantiating it normally without usingstartActivityThis is the line that I assume where you are instantiating your second list (where your
contextis null):If you follow this stacktrace:
In the Android source code, you will see that line 203 is
Where
mBaseis the context that you set inSQLiteOpenHelper.In your code, assuming
DatabasesManageris the one who will interact withSQLiteOpenHelper, it is likelythisthat you pass to it is nullIf you want to use second list in the same Activity, you should consider using the
ListViewdirectly in the code without wrapping it using the anotherListActivity