【发布时间】:2011-04-19 12:54:22
【问题描述】:
我正在尝试从数据库中填充 ListView,并为每一行添加精美的删除按钮。所以我制作了列表 Activity 和自定义 SimpleCursorAdapter。
这是主要的 ListView 活动:
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
}
}
这是自定义的 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);
}
}
当我单击删除按钮时,所有这些东西都会给我 NullPointerException。 我是 android 新手,假设我错过了一些明显的东西。
LogCat:
错误/AndroidRuntime(14027):致命异常:主要
错误/AndroidRuntime(14027): java.lang.NullPointerException
错误/AndroidRuntime(14027):在 android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
错误/AndroidRuntime(14027):在 android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
错误/AndroidRuntime(14027):在命名空间.DBAdapter.open(DBAdapter.java:66)
ERROR/AndroidRuntime(14027): 在命名空间.EditEntries.delRow(EditEntries.java:50)
错误/AndroidRuntime(14027):在命名空间.SMSimpleCursorAdapter$1.onClick(SMSimpleCursorAdapter.java:64)
ERROR/AndroidRuntime(14027): 在 android.view.View.performClick(View.java:2414)
ERROR/AndroidRuntime(14027): 在 android.view.View$PerformClick.run(View.java:8838)
ERROR/AndroidRuntime(14027): 在 android.os.Handler.handleCallback(Handler.java:587)
ERROR/AndroidRuntime(14027):在 android.os.Handler.dispatchMessage(Handler.java:92)
ERROR/AndroidRuntime(14027): 在 android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(14027): 在 android.app.ActivityThread.main(ActivityThread.java:4680)
ERROR/AndroidRuntime(14027): at java.lang.reflect.Method.invokeNative(Native Method)
错误/AndroidRuntime(14027):在 java.lang.reflect.Method.invoke(Method.java:521)
错误/AndroidRuntime(14027):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
错误/AndroidRuntime(14027):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
ERROR/AndroidRuntime(14027): at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
-
你能发布
logcat的输出吗?
标签: android database sqlite adapter android-listview