简体   繁体   中英

Error receiving broadcast intent from broadcast receiver

i have a broadcast receiver, and in onRecive i execute Asynctask that when executed, in preExecute, i show a dialog as a busy indicator. at run time when the AsyncTask executes i receive the below logcat errors, to fix that error i tried to pass getApplicationContext() as a reference to the context to the constructor of the asyncTask but i still receive the same error

please let me know how to fix that error

code :

//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
  • You can not show AlertDialog from Receiver because it needs Activity's Context.

  • Instead you can start an Activity with Theme.Dialog and start AsyncTask that will show an AlertDialog .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM