我试图从我的数据库中的表中清除所有数据,它是成功的,但唯一的问题是,每次我按下清除按钮,它确实清除所有数据但是之后应用程序显示它已经停止
我哪个部分出了问题?以下是我的数据库编码:
public boolean deleteRow(long id) {
SQLiteDatabase db = helper.getWritableDatabase();
String where = data.UID + "=" + id;
return db.delete(data.TABLE_NAME, where, null) != 0;
}
public void DeleteAll(){
Cursor c = readEntry();
long id = c.getColumnIndexOrThrow(data.UID);
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) id));
} while (c.moveToNext());
}
c.close();
return;
}
和来自其他java的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showbmi);
initializeApp();
}
private void initializeApp(){
weightinputid = (EditText) findViewById(R.id.weightid);
heightinputid = (EditText) findViewById(R.id.heightid);
buttonBMI = (Button) findViewById(R.id.buttonBMI);
BMIfinal= (TextView) findViewById(R.id.BMIfinal);
BMIStatus = (TextView) findViewById(R.id.BMIstatus);
save = (Button) findViewById(R.id.button);
detail = (Button)findViewById(R.id.button1);
bb = (Button)findViewById(R.id.bb);
table_layout = (TableLayout) findViewById(R.id.tableLayout1);
data = new fitnessdatabase(this);
BuildTable();
bb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data.DeleteAll();
new MyAsync().execute();
}
});
private class MyAsync extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
table_layout.removeAllViews();
PD = new ProgressDialog(Showbmi.this);
PD.setTitle("Please Wait..");
PD.setMessage("Loading...");
PD.setCancelable(false);
PD.show();
}
@Override
protected Void doInBackground(Void... params) {
String weight = weightinputid.getText().toString();
String bmi = BMIfinal.getText().toString();
String status = BMIStatus.getText().toString();
String date1 = mDateText.getText().toString();
// inserting data
data.open();
// sqlcon.insertData(firstname, lastname);
long id = data.insertData(weight, bmi, status, date1);
BuildTable();
return null;
}
}
来自logcat的错误如下所示:
java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at com.example.adiehabs.fitnessku.Showbmi$MyAsync.doInBackground(Showbmi.java:188)
at com.example.adiehabs.fitnessku.Showbmi$MyAsync.doInBackground(Showbmi.java:169)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
答案 0 :(得分:0)
当您尝试data.Open()
时,它会触及一个未初始化的变量,因为您已经在私人虚空中进行了初始化。将其更改为公开或将所有代码放入您创建的包中。由于UI控件也将为null。
编辑您使用logcat更新后确认了这一点。在背景中做。
引起:java.lang.NullPointerException 在com.example.adiehabs.fitnessku.Showbmi $ MyAsync.doInBackground(Showbmi.java:188)
private void initializeApp(){
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) id));
} while (c.moveToNext());
}
我会用: 将光标移动到第一个,只有在它没有经过最后一个项目时才移动它。
c.moveToFirst();
while(!c.isAfterLast()) {
deleteRow(c.getLong((int) id));
c.moveToNext();
}
}
另一件事:
您是否已在主范围table_layout
,PD
中声明了这些变量?
应用程序没有响应通常来自nullpointer异常。
答案 1 :(得分:0)
删除所有表格行
public boolean deleteTable(){
return mSQLiteDatabase.delete(DataBaseHelper.TABLE, null, null) > 0;
}