android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不适用于应用程序

时间:2014-03-31 18:32:15

标签: android

在我的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

5 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

试试这个AlertDialog.Builder adb=new AlertDialog.Builder(getParent());

答案 2 :(得分:0)

你无法处理来自非ui线程的ui更改。 从报警管理器的onReceive方法调用活动函数(showdialog())。

public void showdialog()
{
  yourActivity.this.runOnUiThread(new Runnable(){
    public void run(){
       // Create a Alert dialog and show it
    }
  });
}

答案 3 :(得分:0)

创建一个Constructor,您可以在其中获取活动。像这样 -

Activity activity;
public AlarmReceiver2 (Activity activity){
         this.activity = activity;
}

现在,使用此activity作为参数,而不是使用arg0,即context

AlertDialog.Builder adb=new AlertDialog.Builder(arg0);

因为只能使用context来显示对话框。您需要为此提供Activity

答案 4 :(得分:0)

尝试此上下文= youractivity.this; 然后使用context而不是getApplicationContext()