0

I'm currently trying to implement a custom loading dialog, However when I try to inflate my layout in the PreExecute of an AsyncTask the app crashes with a BadTokenException. I haven't been able to figure out whats causing this problem. The error is specifically at the popupWindow.showAtLocation(view,Gravity.CENTER, 0, 0); line.

The ProgressDialog class:

public class ProgressDialog {
    private Context mContext;
    private PopupWindow popupWindow;
    private TextView details;
    private TextView loadingText;
    private View view;
    public ProgressDialog(Context c, View view) {
        this.mContext = c;
        this.view = view;
    }
    public void show(String message){ //copied this off an answer to a different question
        // inflate the layout of the popup window
        LayoutInflater inflater = (LayoutInflater)
               mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = inflater.inflate(R.layout.custom_loading, null);
        // create the popup window
        int width = LinearLayout.LayoutParams.WRAP_CONTENT;
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;
        boolean focusable = true; // lets taps outside the popup also dismiss it
        popupWindow = new PopupWindow(popupView, width, height, focusable);
        popupWindow.setElevation(20);
        popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(mContext,R.drawable.transparent_back));
        // show the popup window
        // which view you pass in doesn't matter, it is only used for the window token
        loadingText = popupView.findViewById(R.id.dialogTitle);
        loadingText.setText(R.string.loadingString);
        details = popupView.findViewById(R.id.dialogInfo);
        details.setText(message);
        popupWindow.showAtLocation(view,Gravity.CENTER, 0, 0);
    }
    public void dismiss(){
        popupWindow.dismiss();
    }
}

The Activity:

private ProgressDialog prg;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_splash);
    AdView mAdView = findViewById(R.id.adView);
    prg = new ProgressDialog(this, mAdView);
    //Do some stuff
}
public void ReadSongs()
{
        AsyncTask<Void,Void,Void> task = new AsyncTask<Void, Void, Void>(){
            @Override
            protected void onPreExecute(){
                prg.show("Some Message");
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... voids){
                //Do Stuff
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid){
                prg.dismiss();
                startMainActivity();
                super.onPostExecute(aVoid);
            }
        }.execute();
}

The LogCat:

PID: 10363 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myproject/com.example.myproject.SpotifySongReadAct}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:907)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:387)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
        at android.widget.PopupWindow.invokePopup(PopupWindow.java:1577)
        at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1343)
        at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1309)
        at utility.ProgressDialog.show(ProgressDialog.java:46)
        at com.barbecu.switcheroo.SpotifySongReadAct$1.onPreExecute(SpotifySongReadAct.java:50)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:708)
        at android.os.AsyncTask.execute(AsyncTask.java:655)
        at com.barbecu.switcheroo.SpotifySongReadAct.ReadSongs(SpotifySongReadAct.java:100)
        at com.barbecu.switcheroo.SpotifySongReadAct.onCreate(SpotifySongReadAct.java:114)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
barbecu
  • 684
  • 10
  • 28
  • you are using `.showAtLocation(view,Gravity.CENTER, 0, 0)` and you're not specifying a container in `inflate()` – Alan Deep May 17 '20 at 20:30
  • Does this answer your question? [PopupWindow $BadTokenException: Unable to add window -- token null is not valid](https://stackoverflow.com/questions/8782250/popupwindow-badtokenexception-unable-to-add-window-token-null-is-not-valid) – Jenea Vranceanu May 17 '20 at 20:31
  • @AlanDeep The pop-up works fine when its not run in an async task though, If this is the problem what container should I specify? – barbecu May 17 '20 at 20:43
  • @JeneaVranceanu both the runnable and the isFinishing solutions didn't work – barbecu May 17 '20 at 21:13
  • @barbecu The link I gave you solves the issue, look closely but only at accepted answer. It says that you attempt to show popup too early. `showAtLocation` method requires the first argument (the `View`) to be attached to the screen already. If you check its `windowToken` it will be `null`, and if you want popup to show it must not be null. I see by looking at stacktrace that you call `ReadSongs()` from `onCreate` method. It is too early. You must move it to `Activity#onAttachedToWindow()` at least. – Jenea Vranceanu May 18 '20 at 08:24
  • Thanks! Fixed it! Ended up attaching a runnable to the view and calling the main function from the runnable, If you post an answer I'd be happy to accept. – barbecu May 18 '20 at 18:38

0 Answers0