从广播接收器接收广播意图时出错

时间:2016-02-16 10:42:08

标签: android android-asynctask broadcastreceiver android-broadcast android-broadcastreceiver

我有一个广播接收器,并且在onRecive中我执行Asynctask,执行时,在preExecute中,我显示一个对话框作为忙指示符。在运行时 AsyncTask执行我收到以下logcat错误,修复该错误我试图将getApplicationContext()作为对上下文的引用传递给 asyncTask的构造函数,但我仍然收到相同的错误

请让我知道如何解决该错误

//executingt the AsyncTask from onReceive
if (sppBondState == BluetoothDevice.BOND_BONDED) {
    Log.d(TAG, SubTag.bullet("mBCR_STATE_CHANGED", "unbonding spp wil start"));
    ATSPPUnbond mATSPPUnbond = new ATSPPUnbond(getApplicationContext());
    mATSPPUnbond.execute();
}
...
...
...
 @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Log.w(TAG, SubTag.msg("ATSPPUnbond.onPreExecute"));
        ...
        ...
        ...
        this.mDialog = new Dialog(this.mCtx);
        this.mDialog.setCancelable(false);
        this.mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.mDialog.setContentView(R.layout.dialog_phases);

        this.mtvPhaseName = (TextView) this.mDialog.findViewById(R.id.act_main_tv_phase_name);
        this.mpbBusyIndicator = (ProgressBar) this.mDialog.findViewById(R.id.act_main_pb_busy_indicator);

        this.mtvPhaseName.setText(getApplicationContext().getResources().getString(R.string.phase_unbonding_from_remote_device));

        this.mDialog.show(); //line 776
    }

logcat的

FATAL EXCEPTION: main
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime: Process: com.example.com.vpager_00, PID: 28161
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.adapter.action.STATE_CHANGED flg=0x4000010 (has extras) } in com.example.com.ecoassistant_03.ActMain$1@c73928a
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:933)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5938)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:  Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at android.view.ViewRootImpl.setView(ViewRootImpl.java:691)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:288)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at android.app.Dialog.show(Dialog.java:312)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at com.example.com.ecoassistant_03.ActMain$ATSPPUnbond.onPreExecute(ActMain.java:776)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at android.os.AsyncTask.execute(AsyncTask.java:535)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at com.example.com.ecoassistant_03.ActMain$1.onReceive(ActMain.java:123)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:923)
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739) 
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145) 
02-16 11:23:08.305 28161-28161/com.example.com.vpager_00 E/AndroidRuntime:     at android.app

1 个答案:

答案 0 :(得分:0)

  • 您无法从AlertDialog显示Receiver,因为它需要活动的上下文。

  • 您可以使用Activity开始Theme.Dialog并开始显示AsyncTask的{​​{1}}。