我有一个广播接收器,在 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
}
日志猫:
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