运行时异常 - 无法启动接收器

时间:2014-06-11 10:05:15

标签: android broadcastreceiver

当我使用Toast消息时,它正在工作。现在当我用警告对话框替换它时,我得到了运行时异常。这是我的代码:

public class ConnectionReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
        if (activeNetwork != null && activeNetwork.isConnected()) {
//          Toast.makeText(context, "Internet Connection is Active",
//                  Toast.LENGTH_LONG).show();
            AlertDialog successAlert=new AlertDialog.Builder(context).create();
            successAlert.setMessage("Internet Connection is Active");
            successAlert.show();

        } else {
              AlertDialog failureAlert=new AlertDialog.Builder(context).create();
              failureAlert.setMessage("Internet Connection Timed Out....Please try again");
              failureAlert.show();
        }

    }
}

它正确地显示了Toast消息。现在,当我添加警报对话框时,我的应用程序崩溃了。无法弄清楚原因。警报对话框中有什么问题吗?

Log Cat条目:

06-11 06:02:02.010: W/dalvikvm(3823): threadid=1: thread exiting with uncaught exception (group=0xb2abbba8)
06-11 06:02:02.040: E/AndroidRuntime(3823): FATAL EXCEPTION: main
06-11 06:02:02.040: E/AndroidRuntime(3823): Process: com.example.finalassignment, PID: 3823
06-11 06:02:02.040: E/AndroidRuntime(3823): java.lang.RuntimeException: Unable to start receiver com.example.finalassignment.ConnectionReciever: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
06-11 06:02:02.040: E/AndroidRuntime(3823):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2426)
06-11 06:02:02.040: E/AndroidRuntime(3823):     at android.app.ActivityThread.access$1700(ActivityThread.java:135)
06-11 06:02:02.040: E/AndroidRuntime(3823):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
06-11 06:02:02.040: E/AndroidRuntime(3823):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-11 06:02:02.040: E/AndroidRuntime(3823):     at android.os.Looper.loop(Looper.java:136)
06-11 06:02:02.040: E/AndroidRuntime(3823):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-11 06:02:02.040: E/AndroidRuntime(3823):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 06:02:02.040: E/AndroidRuntime(3823):     at java.lang.reflect.Method.invoke(Method.java:515)
06-11 06:02:02.040: E/AndroidRuntime(3823):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-11 06:02:02.040: E/AndroidRuntime(3823):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-11 06:02:02.040: E/AndroidRuntime(3823):     at dalvik.system.NativeStart.main(Native Method)
06-11 06:02:02.040: E/AndroidRuntime(3823): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
06-11 06:02:02.040: E/AndroidRuntime(3823):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:540)
06-11 06:02:02.040: E/AndroidRuntime(3823):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
06-11 06:02:02.040: E/AndroidRuntime(3823):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
06-11 06:02:02.040: E/AndroidRuntime(3823):     at android.app.Dialog.show(Dialog.java:286)
06-11 06:02:02.040: E/AndroidRuntime(3823):     at com.example.finalassignment.ConnectionReciever.onReceive(ConnectionReciever.java:25)
06-11 06:02:02.040: E/AndroidRuntime(3823):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2419)

4 个答案:

答案 0 :(得分:2)

问题在于活动背景。你需要传递活动的上下文来显示对话框。

答案 1 :(得分:0)

作为Context的参数传递的onReceive()是-ReceiverRestrictedContext

要显示对话框,您需要Context,即Activity

要显示来自广播接收器的通知,请使用Notification而不是对话框。

如果您在清单中注册了收件人,请不要尝试传递Activity引用。一旦onReceive()返回,系统就可以自由地终止您的进程 - 您不能在那里执行任何异步操作(例如Dialog)。

答案 2 :(得分:0)

在我的情况下,当父视图尚未准备好显示并且视图尚未附加到窗口时,如果添加popupo窗口或对话框,则可能会发生这种情况。

解决方案是在触发弹出/对话框之前检查视图是否附加到窗口,或者将弹出窗口延迟几百毫秒。

希望它有所帮助。

答案 3 :(得分:0)

: Unable to add window -- token null is not for an application

当操作系统无法决定哪个活动与调用的窗口相关时发生,在这种情况下为Alert对话框。现在,您正在尝试使用从广播投射接收器传递的上下文,它仍然不会准确指示警报对话框应附加到哪个活动。相反,使用MyActivity.this作为警报对话框中的上下文,一切都会正常工作。

将代码修改为:

public class ConnectionReciever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {
 //          Toast.makeText(context, "Internet Connection is Active",
 //                  Toast.LENGTH_LONG).show();

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MyActivity.this);
        // Replace MyActivity with your activity name in MyActivity.this
        alertDialog.setMessage("Internet Connection is Active");
        alertDialog.show();

    } else {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MyProfile.this);
        // Replace MyActivity with your activity name in MyActivity.this
        alertDialog.setMessage("Internet Connection Timed Out....Please try again");
        alertDialog.show();

    }

}
}