0

I started a project with many UIs. This leads to have a tutorial screen. Therefore, I built a popUpwindow for the first use. But I had some troubles with popupWindow. I added the following code to create it:

 SharedPreferences sharedPreference14 = getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    boolean isFirstLaunched = sharedPreference14.getBoolean("IS_FIRTS_LAUNCHER",false);

    if (isFirstLaunched==false){

        LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View layout = inflater.inflate(R.layout.custompopup,null);
        float density=MainActivity.this.getResources().getDisplayMetrics().density;

        final PopupWindow pw = new PopupWindow(layout, (int)density*285, (int)density*285, true);
        pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
        Button btnNext =(Button) layout.findViewById(R.id.btnNext);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pw.dismiss();
            }
        });
      }
    SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("IS_FIRTS_LAUNCHER",true);
    editor.commit();

However, app crashed. Logcat:

ejava.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.manh_dev.darkygenbmi/com.example.manh_dev.darkygenbmi.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
                                                                                at android.app.ActivityThread.access$600(ActivityThread.java:130)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                at android.os.Looper.loop(Looper.java:137)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                                at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                                at dalvik.system.NativeStart.main(Native Method)
                                                                             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:585)
                                                                                at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
                                                                                at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
                                                                                at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
                                                                                at android.view.Window$LocalWindowManager.addView(Window.java:547)
                                                                                at android.widget.PopupWindow.invokePopup(PopupWindow.java:988)
                                                                                at android.widget.PopupWindow.showAtLocation(PopupWindow.java:845)
                                                                                at android.widget.PopupWindow.showAtLocation(PopupWindow.java:809)
                                                                                at com.example.manh_dev.darkygenbmi.MainActivity.onCreate(MainActivity.java:211)
                                                                                at android.app.Activity.performCreate(Activity.java:5008)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                                                                                at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                at android.os.Looper.loop(Looper.java:137) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:4745) 
                                                                                at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                                                                                at dalvik.system.NativeStart.main(Native Method) 

What's wrong and how to solve it? (The popupWindow sounds like a tutorial for the first use!). A here is a part of my .xml file(custompopup.xml):

<?xml version="1.0" encoding="utf-8"?><RelativeLayout android:layout_width="250dp"
android:layout_height="250dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center"
android:background="@color/colorPrimaryDark"
android:id="@+id/poup"
>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    android:layout_marginRight="6dp"
    android:text="Next"
    android:layout_marginBottom="7dp"
    android:id="@+id/btnNext"
    />

ADM
  • 20,406
  • 11
  • 52
  • 83
  • Why just do not use AlertDialog for this case? – Dmitriy Mitiai Jul 30 '18 at 11:43
  • 3
    Possible duplicate of [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) – Hemant Parmar Jul 30 '18 at 11:43

1 Answers1

0

You are showing the popup too early, you need to defer it, until all the lifecycle methods finish, add a delay to avoid BadTokenException.

Try this (this is not the best solution):

new Handler().postDelayed(new Runnable() {
    public void run() {
        pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
    }
}, 100);
Belbahar Raouf
  • 760
  • 1
  • 4
  • 17