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)
6
  • you are using .showAtLocation(view,Gravity.CENTER, 0, 0) and you're not specifying a container in inflate() Commented May 17, 2020 at 20:30
  • Does this answer your question? PopupWindow $BadTokenException: Unable to add window -- token null is not valid Commented May 17, 2020 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? Commented May 17, 2020 at 20:43
  • @JeneaVranceanu both the runnable and the isFinishing solutions didn't work Commented May 17, 2020 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. Commented May 18, 2020 at 8:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.