請看看這段代碼,看看你能不能幫我一把。 viewItem從數據庫填充,並且方法fillRemindersData(long rowId)僅從提醒類中調用。我不知道爲什麼我調用viewitems.fillRemindersData()方法時得到一個nullPointerException。如果我評論該行,代碼工作正常,rowId是正確的。可能是什麼原因?感謝NullPointException不知道爲什麼?
// this is a reminder class
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_list);
adapter = new DBAdapter(this);
viewItems = new ViewItems();
fillReminder();
}
private void fillReminder() {
Bundle extra = getIntent().getExtras();
if(extra != null){
rowId = extra.getLong(DBAdapter.KEY_ID);
message = extra.getString(NewItem.Test);
}
Toast.makeText(this, message + "its ok here" + rowId, Toast.LENGTH_LONG).show();
viewItems.fillRemindersData(rowId); // having a null pointer exception here.
}
而且viewItems類的重要組成部分是:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_list);
adapter = new DBAdapter(this);
adapter.open();
fillData();
list = (ListView)findViewById(android.R.id.list);
list.setOnItemClickListener(this);
}
protected void fillData() {
Cursor c = adapter.retrieveItems(); // method in sqlitedatabase
startManagingCursor(c);
String[] from = new String[]{DBAdapter.NAME, DBAdapter.START_DATE, DBAdapter.START_TIME};
int[] to = new int[]{R.id.viewNameId, R.id.viewDateId, R.id.viewTimeId};
customCursorAdapter items = new customCursorAdapter(this, R.layout.view_items, c, from, to);
setListAdapter(items);
}
protected void fillRemindersData(long Id) {
long row = Id;
Cursor c = adapter.retrieveRow(row); // method in sqlitedatabase
startManagingCursor(c);
String[] from = new String[]{DBAdapter.NAME, DBAdapter.START_DATE, DBAdapter.START_TIME};
int[] to = new int[]{R.id.RemindNameId, R.id.remindDateId, R.id.remindTimeId};
customCursorAdapter items = new customCursorAdapter(this, R.layout.remind_viewer, c, from, to);
setListAdapter(items);
}
方法的logcat指的是:
public Cursor retrieveRow(long rowId){
String[] resultColumns = new String[] {NAME,START_DATE,START_TIME};
Cursor row = db.query(true,DATABASE_TABLE, resultColumns, KEY_ID +"=" +rowId, null, null, null, null,null);
if(row != null){
row.moveToNext();
return row;
}
return row;
}
logcat的輸出:
09-22 15:19:00.346: ERROR/AndroidRuntime(808): Uncaught handler: thread main exiting due to uncaught exception
09-22 15:19:00.386: ERROR/AndroidRuntime(808): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MemoBuzz/com.MemoBuzz.RemindViewer}: java.lang.NullPointerException
09-22 15:19:00.386: ERROR/AndroidRuntime(808): Caused by: java.lang.NullPointerException
09-22 15:19:00.386: ERROR/AndroidRuntime(808): at com.MemoBuzz.ViewItems.fillRemindersData(ViewItems.java:57)
09-22 15:19:00.386: ERROR/AndroidRuntime(808): at com.MemoBuzz.RemindViewer.fillReminder(RemindViewer.java:51)
09-22 15:19:00.386: ERROR/AndroidRuntime(808): at com.MemoBuzz.RemindViewer.onCreate(RemindViewer.java:36)
09-22 15:19:00.386: ERROR/AndroidRuntime(808): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
09-22 15:19:00.386: ERROR/AndroidRuntime(808): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
09-22 15:19:00.386: ERROR/AndroidRuntime(808): ... 11 more
stacktrace怎麼樣,fillRemindersData中的哪行是發生異常? – schwiz 2010-09-22 16:15:40
請發佈LogCat輸出。 – 2010-09-22 16:15:44