1

in my App, i am trying to show a busy indicator while i am connecting to a server, so I created AsynchTask with dialogwith progressBar.

At run time when i try to connect to the server, the App crashs, out of the below posted LogCat it seems it is a problem with the dialog but i do not know what causes the error exactly.

the app is in LandScape mode with navigationdrawer

please have a look at the onPreExecute method and the logcat errors.

onPreExecute:

@Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        dialog = new Dialog(getApplicationContext());
        dialog.setCancelable(false);
        //dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.progressdialog);
        progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar1);
        dialog.show();
    }

LogCat:

 E/AndroidRuntime(20575): Process: com.example.mqtt_designlayout_02, PID: 20575
 E/AndroidRuntime(20575): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
 E/AndroidRuntime(20575):   at android.view.ViewRootImpl.setView(ViewRootImpl.java:731)
 E/AndroidRuntime(20575):   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:278)
 E/AndroidRuntime(20575):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
 E/AndroidRuntime(20575):   at android.app.Dialog.show(Dialog.java:288)
 E/AndroidRuntime(20575):   at com.example.mqtt_designlayout_02.MainActivity$BusyIndicator.onPreExecute(MainActivity.java:768)
 E/AndroidRuntime(20575):   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
 E/AndroidRuntime(20575):   at android.os.AsyncTask.execute(AsyncTask.java:535)
 E/AndroidRuntime(20575):   at com.example.mqtt_designlayout_02.MainActivity.connectionIndicator(MainActivity.java:468)
 E/AndroidRuntime(20575):   at com.example.mqtt_designlayout_02.MainActivity.MQTTConnect(MainActivity.java:460)
 E/AndroidRuntime(20575):   at com.example.mqtt_designlayout_02.MainActivity.MQTT_Connection_Module(MainActivity.java:558)
 E/AndroidRuntime(20575):   at com.example.mqtt_designlayout_02.MainActivity.access$0(MainActivity.java:548)
 E/AndroidRuntime(20575):   at com.example.mqtt_designlayout_02.MainActivity$1.onClick(MainActivity.java:79)
 E/AndroidRuntime(20575):   at android.view.View.performClick(View.java:4626)
 E/AndroidRuntime(20575):   at android.view.View$PerformClick.run(View.java:19293)
 E/AndroidRuntime(20575):   at android.os.Handler.handleCallback(Handler.java:733)
 E/AndroidRuntime(20575):   at android.os.Handler.dispatchMessage(Handler.java:95)
 E/AndroidRuntime(20575):   at android.os.Looper.loop(Looper.java:157)
 E/AndroidRuntime(20575):   at android.app.ActivityThread.main(ActivityThread.java:5293)
 E/AndroidRuntime(20575):   at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(20575):   at java.lang.reflect.Method.invoke(Method.java:515)
 E/AndroidRuntime(20575):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
 E/AndroidRuntime(20575):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
 E/AndroidRuntime(20575):   at dalvik.system.NativeStart.main(Native Method)

i am sorry but i am banned from and. so your solution is just, Change following line, dialog = new Dialog(getParent());

user2121
  • 6,401
  • 13
  • 57
  • 121

2 Answers2

5

Have a look over this blog post: http://possiblemobile.com/2013/06/context/ , and scroll down where the comparison between different contexts is shown.

You are using application context:
dialog = new Dialog(getApplicationContext());
to show the dialog, but app context can't do that.

Use instead the activity context.

Update. With reference to your comment:
You can pass an activity instance to the AsyncTask by making your constructor accept a Context parameter.

Eg.:

public class MyAsyncTask extends AsyncTask<>{
    private Context context;

    public MyAsyncTask(Context context){
        this.context = context;
    }

   // and then in onPreExecute use that context
   // dialog = new Dialog(context);
}

And you call your AsyncTask from the activity like this:

new MyAsyncTask(this).execute(); 
// 'this' is the current instance of Activity.
// and Activity extends Context, so, it IS a Context.
Andy Res
  • 15,182
  • 3
  • 55
  • 89
0

My 2 cents. onPreExecute() method may not have an access to the context in case if the activity is about to finish. Have a look at the following answer.

Community
  • 1
  • 1
JackAW
  • 154
  • 2
  • 14