我需要在活动开始后立即显示弹出窗口,但是我最终导致应用程序崩溃,我当前的代码:
public class GameActivity extends SherlockActivity {
Button firstAnswer;
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
...
firstAnswer = (Button) findViewById(R.id.firstanswerbtn);
...
}
protected void onStart() {
super.onStart();
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.playpopup, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button play = (Button) popupView.findViewById(R.id.letsplay);
play.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
playRound();
popupWindow.dismiss();
}
});
popupWindow.showAtLocation(firstAnswer, Gravity.CENTER, 0, 0);
}
...
}
但是我最终崩溃了:
12-02 22:27:27.534: E/AndroidRuntime(28142): java.lang.RuntimeException: Unable to start activity ComponentInfo{jabu.com.languagetemplate/jabu.com.languagetemplate.GameActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
12-02 22:27:27.534: E/AndroidRuntime(28142): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
12-02 22:27:27.534: E/AndroidRuntime(28142): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-02 22:27:27.534: E/AndroidRuntime(28142): at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-02 22:27:27.534: E/AndroidRuntime(28142): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-02 22:27:27.534: E/AndroidRuntime(28142): at android.os.Handler.dispatchMessage(Handler.java:102)
12-02 22:27:27.534: E/AndroidRuntime(28142): at android.os.Looper.loop(Looper.java:135)
12-02 22:27:27.534: E/AndroidRuntime(28142): at android.app.ActivityThread.main(ActivityThread.java:5221)
12-02 22:27:27.534: E/AndroidRuntime(28142): at java.lang.reflect.Method.invoke(Native Method)
12-02 22:27:27.534: E/AndroidRuntime(28142): at java.lang.reflect.Method.invoke(Method.java:372)
12-02 22:27:27.534: E/AndroidRuntime(28142): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-02 22:27:27.534: E/AndroidRuntime(28142): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-02 22:27:27.534: E/AndroidRuntime(28142): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
12-02 22:27:27.534: E/AndroidRuntime(28142): at android.view.ViewRootImpl.setView(ViewRootImpl.java:562)
12-02 22:27:27.534: E/AndroidRuntime(28142): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272)
12-02 22:27:27.534: E/AndroidRuntime(28142): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
12-02 22:27:27.534: E/AndroidRuntime(28142): at android.widget.PopupWindow.invokePopup(PopupWindow.java:1058)
12-02 22:27:27.534: E/AndroidRuntime(28142): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:887)
12-02 22:27:27.534: E/AndroidRuntime(28142): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:851)
12-02 22:27:27.534: E/AndroidRuntime(28142): at package.GameActivity.onStart(GameActivity.java:142)
12-02 22:27:27.534: E/AndroidRuntime(28142): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
12-02 22:27:27.534: E/AndroidRuntime(28142): at android.app.Activity.performStart(Activity.java:5949)
12-02 22:27:27.534: E/AndroidRuntime(28142): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
12-02 22:27:27.534: E/AndroidRuntime(28142): ... 10 more
第142行是:
popupWindow.showAtLocation(firstAnswer, Gravity.CENTER, 0, 0);
答案 0 :(得分:4)
您可以尝试以下内容:
firstAnswer.post(new Runnable() {
public void run() {
pw.showAtLocation(firstAnswer, Gravity.CENTER, 0, 0);
}
});
即使您在onCreate方法中设置主要布局,它还不够。您需要对此操作进行排队,直到所有内容都准备就绪。
希望这有帮助。