我正在尝试删除当前存储在数据库中的所有条目,因此我设置了按钮“bDeleteTimings”,并且我希望在它被用作确认时显示AlertDialog删除。我是SQLite和AlertDialogs的新手,所以请帮助我们。我在“tdb.deleteEntries”行上得到一个NullPointerException。感谢。
这是我按下bDeleteTimings时使用的代码:
case R.id.bDeleteTimings:
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Delete Timings")
.setMessage("Are you sure you want to delete all currently set timings? (Cannot be undone)")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
tdb.deleteEntries();
}
}).setNegativeButton("No", null).show();
break;
}//end switch
这是我的数据库代码:
public class TimingsDb {
public static final String KEY_ID = "_id";
public static final String KEY_SUBJECT = "subject_name";
public static final String KEY_WEEK = "week";
public static final String KEY_DAY = "day_name";
public static final String KEY_LESSON = "lesson";
private static final String DATABASE_NAME = "TimingsDB";
private static final String DATABASE_TABLE = "TimingsTable";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}// end Constructor
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_SUBJECT
+ " TEXT, " + KEY_WEEK + " INTEGER, " + KEY_DAY + " TEXT, "
+ KEY_LESSON + " INTEGER);");
}// end onCreate
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}// end onUpgrade
}// end class
public TimingsDb(Context c) {
ourContext = c;
}
public TimingsDb open() throws SQLException {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}// end open
public void close() {
ourHelper.close();
}// end close
public void deleteEntries(){
ourDatabase.execSQL("DELETE * FROM "+DATABASE_TABLE+";");
}//end deleteEntries
} //结束TimingsDb
(这个类中显然还有其他方法,但我删除了它们以避免并发症)
(编辑)这是LogCat:
02-27 19:31:30.040: E/AndroidRuntime(8044): FATAL EXCEPTION: main
02-27 19:31:30.040: E/AndroidRuntime(8044): java.lang.NullPointerException
02-27 19:31:30.040: E/AndroidRuntime(8044): at com.shearer.schooltimetable.TimingsDb.deleteEntries(TimingsDb.java:86)
02-27 19:31:30.040: E/AndroidRuntime(8044): at com.shearer.schooltimetable.EditTimetable$1.onClick(EditTimetable.java:55)
02-27 19:31:30.040: E/AndroidRuntime(8044): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
02-27 19:31:30.040: E/AndroidRuntime(8044): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 19:31:30.040: E/AndroidRuntime(8044): at android.os.Looper.loop(Looper.java:137)
02-27 19:31:30.040: E/AndroidRuntime(8044): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-27 19:31:30.040: E/AndroidRuntime(8044): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 19:31:30.040: E/AndroidRuntime(8044): at java.lang.reflect.Method.invoke(Method.java:511)
02-27 19:31:30.040: E/AndroidRuntime(8044): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-27 19:31:30.040: E/AndroidRuntime(8044): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-27 19:31:30.040: E/AndroidRuntime(8044): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
ourDatabase
中{p> null
为deleteEntries()
。由于您在open()
初始化它,似乎您还没有打开数据库。您也可以在getWritableDatabase()
中拨打deleteEntries()
。
此外,还存在SQL语法问题。移除*
中的DELETE * FROM...
。您只能将*
列通配符与SELECT
s。
答案 1 :(得分:0)
你可以简单地放下桌子。
case R.id.bDeleteTimings:
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Delete Timings")
.setMessage("Are you sure you want to delete all currently set timings? (Cannot be undone)")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
SQLiteDatabase mDB;
DbHelper dbhelper = new DbHelper(this);
mDB = dbhelper.getWritableDataBase();
mDB.execSQL("DROP TABLE "+DbHelper.DATABASE_TABLE);
dbhelper.onCreate(mDB);
}
}).setNegativeButton("No", null).show();
break;
}//end switch
答案 2 :(得分:-1)
使用此
db.delete(DATABASE_TABLE, null, null);