我在 onClickListener 中有一个带有 Spinner 的应用程序。该代码适用于早期的 API,例如 API 8、9 和 10。它与 API 13、14、15、16 和 17 一起使用。错误消息是:
02-01 20:33:03.298: E/AndroidRuntime(812): FATAL EXCEPTION: main
02-01 20:33:03.298: E/AndroidRuntime(812): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@40cf4bc0 is not valid; is your activity running?
是什么导致应用程序爆炸?代码如下:
package com.example.spinerr;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button settingsB;
private Spinner mainS, settingsS;
private String[] mainItems = { "Main 1", "Main 2", "Main 3", "Main 4" };
private String[] settingsItems = { "Entry 1", "Entry 2", "Entry 3" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsB = (Button) findViewById(R.id.Settings);
settingsB.setOnClickListener(onSettings);
mainS = (Spinner) findViewById(R.id.mainSpinner);
@SuppressWarnings({ "rawtypes", "unchecked" })
ArrayAdapter adapterM = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, mainItems);
mainS.setAdapter(adapterM);
mainS.setPrompt("Pick item:");
mainS.setSelection(2);
mainS.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@SuppressWarnings("rawtypes")
public void onItemSelected(AdapterView adapter4, View v, int i,
long lng) {
popUp("in mainS onItemSelected");
}
public void onNothingSelected(
@SuppressWarnings("rawtypes") AdapterView arg0) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private View.OnClickListener onSettings = new View.OnClickListener() {
@SuppressWarnings("rawtypes")
public void onClick(View v) {
popUp("in onSettings");
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.pusettings, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
settingsS = (Spinner) popupView.findViewById(R.id.settingsSpinner);
@SuppressWarnings("unchecked")
ArrayAdapter<CharSequence> tAdapter = new ArrayAdapter(
getBaseContext(), android.R.layout.simple_spinner_item,
settingsItems);
settingsS.setAdapter(tAdapter);
settingsS.setPrompt("Pick an entry:");
settingsS.setSelection(0);
settingsS.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView adapter1,
View v, int i, long lng) {
popUp("In settings onItemSelected.");
}
public void onNothingSelected(AdapterView arg0) {
}
});
popupWindow.showAsDropDown(settingsB, 1, 1);
}
};
private void popUp(String s) {
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}
}
activity_main.XML 如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/Lime"
>
<Button android:id="@+id/Settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/settings"
android:typeface="serif"
android:textSize="14sp"
android:textStyle="bold"
android:width="80dp"
android:height="36dp"
/>
<Spinner
android:id="@+id/mainSpinner"
android:prompt="@string/pick"
android:text="@string/press"
android:minWidth="80dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>
pusettings.xml 如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Gold"
android:orientation="vertical" >
<LinearLayout
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/Citrus"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="@string/settings" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="10dp"
android:layout_height="fill_parent" />
<Spinner
android:id="@+id/settingsSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="10dp" />
</LinearLayout>
</LinearLayout>
感谢您提供的任何帮助!
添加--完整的堆栈跟踪:
02-02 00:27:28.459: E/AndroidRuntime(624): FATAL EXCEPTION: main
02-02 00:27:28.459: E/AndroidRuntime(624): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@41087988 is not valid; is your activity running?
02-02 00:27:28.459: E/AndroidRuntime(624): at android.view.ViewRootImpl.setView(ViewRootImpl.java:515)
02-02 00:27:28.459: E/AndroidRuntime(624): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:279)
02-02 00:27:28.459: E/AndroidRuntime(624): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
02-02 00:27:28.459: E/AndroidRuntime(624): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
02-02 00:27:28.459: E/AndroidRuntime(624): at android.view.Window$LocalWindowManager.addView(Window.java:537)
02-02 00:27:28.459: E/AndroidRuntime(624): at android.widget.PopupWindow.invokePopup(PopupWindow.java:988)
02-02 00:27:28.459: E/AndroidRuntime(624): at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:897)
02-02 00:27:28.459: E/AndroidRuntime(624): at android.widget.ListPopupWindow.show(ListPopupWindow.java:595)
02-02 00:27:28.459: E/AndroidRuntime(624): at android.widget.Spinner$DropdownPopup.show(Spinner.java:739)
02-02 00:27:28.459: E/AndroidRuntime(624): at android.widget.Spinner.performClick(Spinner.java:435)
02-02 00:27:28.459: E/AndroidRuntime(624): at android.view.View$PerformClick.run(View.java:13983)
02-02 00:27:28.459: E/AndroidRuntime(624): at android.os.Handler.handleCallback(Handler.java:605)
02-02 00:27:28.459: E/AndroidRuntime(624): at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 00:27:28.459: E/AndroidRuntime(624): at android.os.Looper.loop(Looper.java:137)
02-02 00:27:28.459: E/AndroidRuntime(624): at android.app.ActivityThread.main(ActivityThread.java:4340)
02-02 00:27:28.459: E/AndroidRuntime(624): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 00:27:28.459: E/AndroidRuntime(624): at java.lang.reflect.Method.invoke(Method.java:511)
02-02 00:27:28.459: E/AndroidRuntime(624): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-02 00:27:28.459: E/AndroidRuntime(624): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-02 00:27:28.459: E/AndroidRuntime(624): at dalvik.system.NativeStart.main(Native Method)