我编写了一个与SQLite DB通信的程序。现在,我正在尝试添加一个窗口,以确认用户是否确实要清除数据库,但是我有一些错误。这是我的第一个窗口警报对话框。
cleanbtm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage(R.string.confirm_clean_table)
.setCancelable(false)
.setPositiveButton(R.string.afirmative_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dbAdapter dbAdapter = new dbAdapter(getApplicationContext());
dbAdapter.eraseGoods();
}
})
.setNegativeButton(R.string.negative_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
LogCat显示以下错误:
03-13 13:12:49.445: W/KeyCharacterMap(224): No keyboard for id 0
03-13 13:12:49.452: W/KeyCharacterMap(224): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
03-13 13:13:03.212: I/dalvikvm(224): threadid=7: reacting to signal 3
03-13 13:13:03.232: I/dalvikvm(224): Wrote stack trace to '/data/anr/traces.txt'
03-13 13:13:04.522: D/dalvikvm(224): GC freed 3476 objects / 231880 bytes in 86ms
03-13 13:13:14.432: I/global(224): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
03-13 13:13:18.524: W/KeyCharacterMap(224): No keyboard for id 0
03-13 13:13:18.524: W/KeyCharacterMap(224): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
03-13 13:13:23.002: D/AndroidRuntime(224): Shutting down VM
03-13 13:13:23.002: W/dalvikvm(224): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
03-13 13:13:23.002: E/AndroidRuntime(224): Uncaught handler: thread main exiting due to uncaught exception
03-13 13:13:23.024: E/AndroidRuntime(224): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.ViewRoot.setView(ViewRoot.java:460)
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-13 13:13:23.024: E/AndroidRuntime(224): at android.app.Dialog.show(Dialog.java:238)
03-13 13:13:23.024: E/AndroidRuntime(224): at com.remotedata.firstapp.chartView$2.onClick(chartView.java:54)
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.View.performClick(View.java:2344)
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.View.onTouchEvent(View.java:4133)
03-13 13:13:23.024: E/AndroidRuntime(224): at android.widget.TextView.onTouchEvent(TextView.java:6510)
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.View.dispatchTouchEvent(View.java:3672)
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
03-13 13:13:23.024: E/AndroidRuntime(224): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
03-13 13:13:23.024: E/AndroidRuntime(224): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
03-13 13:13:23.024: E/AndroidRuntime(224): at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
03-13 13:13:23.024: E/AndroidRuntime(224): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
03-13 13:13:23.024: E/AndroidRuntime(224): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 13:13:23.024: E/AndroidRuntime(224): at android.os.Looper.loop(Looper.java:123)
03-13 13:13:23.024: E/AndroidRuntime(224): at android.app.ActivityThread.main(ActivityThread.java:4203)
03-13 13:13:23.024: E/AndroidRuntime(224): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 13:13:23.024: E/AndroidRuntime(224): at java.lang.reflect.Method.invoke(Method.java:521)
03-13 13:13:23.024: E/AndroidRuntime(224): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-13 13:13:23.024: E/AndroidRuntime(224): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
03-13 13:13:23.024: E/AndroidRuntime(224): at dalvik.system.NativeStart.main(Native Method)
03-13 13:13:23.032: I/dalvikvm(224): threadid=7: reacting to signal 3
03-13 13:13:23.041: I/dalvikvm(224): Wrote stack trace to '/data/anr/traces.txt'
最佳答案
您需要提供活动作为上下文...。
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
关于java - AlertDialog无法打开,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9684797/