Spinner在以后的API 13+上导致BadTokenException

时间:2013-02-01 20:51:38

标签: android android-layout android-widget android-xml

我在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)

2 个答案:

答案 0 :(得分:0)

当布局首次渲染时(当然还有更改选择时),微调器onItemSelected侦听器将被触发,这在活动生命周期中可能太早,以使toast消息具有良好的上下文。用try-catch包裹你的吐司,看看是否捕获异常,或者是否在其他地方抛出异常。

答案 1 :(得分:0)

BadTokenException在以后的API版本中消失了。不再有问题。