gpt4 book ai didi

java - 警报对话框打不开

转载 作者:行者123 更新时间:2023-12-02 07:55:39 26 4
gpt4 key购买 nike

我编写了一个与 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'

最佳答案

您需要提供 Activity 作为上下文......

AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);

关于java - 警报对话框打不开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9684797/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com