所以,我有兩個班,一個是增加了任務和一個增加的類別。添加任務時,可以將該任務與使用下拉列表填充的類別相關聯,並且數據庫通過使用觸發器強制外鍵。我已經得到了應用程序正確添加任務,但我用我的添加類別類捕獲空指針異常,並且我無法鎖定它的位置 - 我查看了logcat,但它並沒有特別有用。空指針異常插入對SQLite數據庫的時候 - Android電子
類如下:
public class AddCategory extends Activity {
EditText txtCatName;
DatabaseHelper dbHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addcategory);
txtCatName = (EditText) findViewById(R.id.txtCatName);
}
public void btnAddCat_Click(View view) {
boolean ok = true;
try {
String name = txtCatName.getText().toString();
DatabaseMethods cat = new DatabaseMethods(name);
dbHelper.AddCategory(cat);
} catch (Exception ex) {
ok = false;
CatchError(ex.toString());
} finally {
if (ok) {
// NotifyEmpAdded();
Toasts.ShowTaskAddedAlert(this);
}
}
}
void CatchError(String Exception) {
Dialog diag = new Dialog(this);
diag.setTitle("Add new Category");
TextView txt = new TextView(this);
txt.setText(Exception);
diag.setContentView(txt);
diag.show();
}
void NotifyCatAdded() {
Dialog diag = new Dialog(this);
diag.setTitle(this.getString(R.string.add_category));
TextView txt = new TextView(this);
txt.setText(this.getString(R.string.category_added));
diag.setContentView(txt);
diag.show();
try {
diag.wait(1000);
} catch (InterruptedException e) {
CatchError(e.toString());
}
diag.notify();
diag.dismiss();
}
}
如果您需要更多的代碼,那麼請你只問。任何幫助深表感謝。總是有助於讓代碼中的更多對眼睛發現錯誤:)。
雖然你說'logcat'沒有幫助,你會介意呢發佈呢? – 2011-03-08 21:40:41
沒有堆棧跟蹤?你確定txtCatName不是null嗎? – 2011-03-08 21:42:49
我willytate同意,在logcat的,你可以在代碼至極特定行看到你剛開那空指針異常,非常有幫助... – Jorgesys 2011-03-08 21:43:54