Reputation: 3409
I'm trying to make ListView populated from database, and spice each row with fancy delete button. So I made list Activity and custom SimpleCursorAdapter.
This is main ListView activity:
public class EditEntries extends ListActivity {
Cursor cursor;
DBAdapter db = new DBAdapter(this);
private static String[] FROM = { _ID, SCORE_COLUMN,
"date(time, 'localtime')", };
private static int[] TO = { R.id.rowid, R.id.title, R.id.time, };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entries_list);
try {
cursor = getEvents();
showEvents(cursor);
} finally {
db.close();
}
}
private void showEvents(Cursor cursor) {
// Set up data binding
SMSimpleCursorAdapter adapter = new SMSimpleCursorAdapter(this,
R.layout.entries_list_item, cursor, FROM, TO);
setListAdapter(adapter);
}
private Cursor getEvents() {
db.open();
cursor = db.getAllScores();
startManagingCursor(cursor);
return cursor;
}
void delRow() { // this is method for deleting row by _id
db.open();
db.deleteScore(3); // here will be TAG with _id from adapter,
db.close(); // for now I just use hardcoded _id
}
}
And this is custom SimpleCursorAdapter:
public class SMSimpleCursorAdapter extends SimpleCursorAdapter{
Cursor c;
Context context;
Activity activity;
public SMSimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.c = c;
this.context=context;
this.activity=(Activity) context;
}
EditEntries dbDel = new EditEntries(); //from previous code sample
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
convertView = View.inflate(context, R.layout.entries_list_item, null);
View row = convertView;
c.moveToPosition(position);
TextView score = (TextView) convertView.findViewById(R.id.title);
TextView time = (TextView) convertView.findViewById(R.id.time);
TextView id = (TextView) convertView.findViewById(R.id.rowid);
id.setText(c.getString(0));
time.setText(c.getString(2));
score.setText(c.getString(1));
String daTag = c.getString(1);
ImageButton delButton = (ImageButton) convertView.findViewById(R.id.delButton);
delButton.setFocusable(true);
delButton.setClickable(true);
//delButton.setTag(daTag);
delButton.setOnClickListener(new OnClickListener() { //Click listener fro delete button
@Override
public void onClick(View view) {
dbDel.delRow(); //this is "delete row" method from previos code sample
}
});
return(row);
}
}
And all this stuff gives me NullPointerException when I click delete button. I'm new to android, and suppose that I just missed something obvious.
LogCat:
ERROR/AndroidRuntime(14027): FATAL EXCEPTION: main
ERROR/AndroidRuntime(14027): java.lang.NullPointerException
ERROR/AndroidRuntime(14027): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
ERROR/AndroidRuntime(14027): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
ERROR/AndroidRuntime(14027): at namespace.DBAdapter.open(DBAdapter.java:66)
ERROR/AndroidRuntime(14027): at namespace.EditEntries.delRow(EditEntries.java:50)
ERROR/AndroidRuntime(14027): at namespace.SMSimpleCursorAdapter$1.onClick(SMSimpleCursorAdapter.java:64)
ERROR/AndroidRuntime(14027): at android.view.View.performClick(View.java:2414)
ERROR/AndroidRuntime(14027): at android.view.View$PerformClick.run(View.java:8838)
ERROR/AndroidRuntime(14027): at android.os.Handler.handleCallback(Handler.java:587)
ERROR/AndroidRuntime(14027): at android.os.Handler.dispatchMessage(Handler.java:92)
ERROR/AndroidRuntime(14027): at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(14027): at android.app.ActivityThread.main(ActivityThread.java:4680)
ERROR/AndroidRuntime(14027): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(14027): at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(14027): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
ERROR/AndroidRuntime(14027): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
ERROR/AndroidRuntime(14027): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 3
Views: 9647
Reputation: 4081
The problem is in your SMSimpleCursorAdapter code:
EditEntries dbDel = new EditEntries(); //from previous code sample
You create a new object but it won't be a managed Activity (eg. the onCreate method won't be called). The NPE probably comes from your DBAdapter when you try to create it the second time (from your Adapter).
Quick fix:
EditEntries dbDel; //from previous code sample
public SMSimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.c = c;
this.context = context;
this.activity = (Activity) context;
this.dbDel = (EditEntries) context;
}
If you don't want just a quick fix the following would be a much better solution:
Upvotes: 7