gpt4 book ai didi

安卓.view.WindowManager$BadTokenException : Unable to add window — token null is not for an application

转载 作者:行者123 更新时间:2023-11-29 00:21:32 24 4
gpt4 key购买 nike

在我的 android 应用程序中,我在 mainactivity 中使用警报管理器。我正在做的是在特定时间我需要显示一个对话框,显示是否登录。

AlarmReceiver2.java

public class AlarmReceiver2 extends BroadcastReceiver {

@Override
public void onReceive(final Context arg0, Intent arg1) {

Toast.makeText(arg0, "Alarm received!", Toast.LENGTH_LONG).show();

DatabaseHandler1 db = new DatabaseHandler1(arg0 );

int count = db.getRowCount();
if(count == 0){
AlertDialog.Builder adb=new AlertDialog.Builder(arg0);
adb.setTitle("TNO");
adb.setMessage("login?" );
adb.setNegativeButton("Cancel", null);

adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

Intent i = new Intent();
i.setClassName("com.androidhive.pushnotifications", "com.androidhive.pushnotifications.LoginActivity");

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startActivity(i);

}});
adb.show();

}
}

}

警报在正确的时间收到,但应用程序通过在 logcat 中显示错误而崩溃

03-31 14:29:36.899: E/AndroidRuntime(1262): FATAL EXCEPTION: main
03-31 14:29:36.899: E/AndroidRuntime(1262): java.lang.RuntimeException: Unable to start receiver com.androidhive.pushnotifications.AlarmReceiver2: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
03-31 14:29:36.899: E/AndroidRuntime(1262): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2431)
03-31 14:29:36.899: E/AndroidRuntime(1262): at android.app.ActivityThread.access$1500(ActivityThread.java:141)
03-31 14:29:36.899: E/AndroidRuntime(1262): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
03-31 14:29:36.899: E/AndroidRuntime(1262): at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 14:29:36.899: E/AndroidRuntime(1262): at android.os.Looper.loop(Looper.java:137)
03-31 14:29:36.899: E/AndroidRuntime(1262): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-31 14:29:36.899: E/AndroidRuntime(1262): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 14:29:36.899: E/AndroidRuntime(1262): at java.lang.reflect.Method.invoke(Method.java:525)
03-31 14:29:36.899: E/AndroidRuntime(1262): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-31 14:29:36.899: E/AndroidRuntime(1262): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-31 14:29:36.899: E/AndroidRuntime(1262): at dalvik.system.NativeStart.main(Native Method)
03-31 14:29:36.899: E/AndroidRuntime(1262): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
03-31 14:29:36.899: E/AndroidRuntime(1262): at android.view.ViewRootImpl.setView(ViewRootImpl.java:563)
03-31 14:29:36.899: E/AndroidRuntime(1262): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:269)
03-31 14:29:36.899: E/AndroidRuntime(1262): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
03-31 14:29:36.899: E/AndroidRuntime(1262): at android.app.Dialog.show(Dialog.java:281)
03-31 14:29:36.899: E/AndroidRuntime(1262): at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
03-31 14:29:36.899: E/AndroidRuntime(1262): at com.androidhive.pushnotifications.AlarmReceiver2.onReceive(AlarmReceiver2.java:44)
03-31 14:29:36.899: E/AndroidRuntime(1262): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2424)
03-31 14:29:36.899: E/AndroidRuntime(1262): ... 10 more

最佳答案

不用 getApplicationContext(),只需使用 ActivityName.this

关于安卓.view.WindowManager$BadTokenException : Unable to add window — token null is not for an application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22769427/

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