我开始了一个有很多UI的项目。这将导致一个教程屏幕。因此,我第一次使用了popUpwindow。 但是我在popupWindow上遇到了一些麻烦。我添加了以下代码来创建它:
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();
但是,应用程序崩溃了。 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)
怎么了?如何解决? (popupWindow听起来像是第一次使用的教程!)。这是我的.xml文件(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"
/>
答案 0 :(得分:0)
显示弹出窗口为时过早,您需要将其推迟,直到所有生命周期方法完成,再添加一个延迟以避免BadTokenException
。
尝试一下(这不是最佳解决方案):
new Handler().postDelayed(new Runnable() {
public void run() {
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
}, 100);