0

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
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • refer http://stackoverflow.com/questions/1516211/error-receiving-broadcast-intent-problem – sasikumar Feb 16 '16 at 10:48
  • 1
    @user2121: The only way to show UI elements in Android is through Activity. Don't show a progress from Receiver. – kevz Feb 16 '16 at 10:50
  • @sasikumar it might be the same error..but the link you posted addresses other issue and i have checked it – Amrmsmb Feb 16 '16 at 13:24
  • @kevz would you please clarify abit more..becuase sometimes i was able to show UI element on asynctask from another asyncTask using .executeonExecutor – Amrmsmb Feb 16 '16 at 13:35

1 Answers1

0
  • 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.

kevz
  • 2,727
  • 14
  • 39