Android微调器问题

时间:2011-09-15 10:26:25

标签: android spinner

当我点击微调器时,我的应用程序崩溃并抛出类似"android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application "

的异常
 String[] s = new String[60];
        int i;
        for (i = 0; i < 60; i++) {
            s[i] = Integer.toString(i + 1);
        }

        Spinner spin = (Spinner) findViewById(R.id.TimeSpinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, s);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(adapter);
        spin.setOnItemSelectedListener(new MyOnItemSelectedListener());

class MyOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent, View view, int pos,
                long id) {
            spvalue = parent.getItemAtPosition(pos).toString();

        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    }

enter code here logcat的

I/ActivityManager(   66): Displayed activity com.imt.main/.TimeSpinner: 503 ms (total 503 ms)
W/WindowManager(   66): Attempted to add window with non-application token WindowToken{43fc5cf8 token=null}.  Aborting.
D/AndroidRuntime( 1382): Shutting down VM
W/dalvikvm( 1382): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime( 1382): FATAL EXCEPTION: main
E/AndroidRuntime( 1382): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime( 1382):        at android.view.ViewRoot.setView(ViewRoot.java:509)
E/AndroidRuntime( 1382):        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
E/AndroidRuntime( 1382):        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/AndroidRuntime( 1382):        at android.app.Dialog.show(Dialog.java:241)
E/AndroidRuntime( 1382):        at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
E/AndroidRuntime( 1382):        at android.widget.Spinner.performClick(Spinner.java:257)
E/AndroidRuntime( 1382):        at android.view.View$PerformClick.run(View.java:8816)
E/AndroidRuntime( 1382):        at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 1382):        at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 1382):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1382):        at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 1382):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1382):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 1382):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 1382):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 1382):        at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(   66):   Force finishing activity com.imt.main/.TimeSpinner
W/ActivityManager(   66): Activity pause timeout for HistoryRecord{43f92d70 com.imt.main/.TimeSpinner}

3 个答案:

答案 0 :(得分:1)

我面临同样的问题,现在已经解决了。我正在使用GroupActivity作为基于Tab的应用程序。

如果您正在使用GroupActivity,如果出现此问题,那只是因为上下文。您无法将正确的上下文传递给微调器,它会给出错误

现在的解决方案是:

  1. 创建任何布局,例如LinearLayout,代替xml
  2. 中的微调器
  3. 在java代码中动态创建Spinner,如下所示,并将其添加到linearLayout:

    Spinner yourSpinner = new Spinner(getParent()); //动态Spinner yourLinearLayout.addView(yourDriverSpinner); //你的linearlayout

  4. 现在,做任何你想用旋转器做的事。

  5. 这对我有很大帮助,也会帮助你。

    如果有任何错误,请告诉我。

    享受。

    :)

答案 1 :(得分:0)

在类级别将其定义为成员变量

String[] s = new String[60];

替换此行

spvalue = parent.getItemAtPosition(pos).toString();

用这个

spvalue = s[pos];

答案 2 :(得分:0)

这里是简单的微调器示例..

<强> spinner.java

package com.spinner;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class spinner extends Activity {
/** Called when the activity is first created. */
private String array_spinner[];

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

array_spinner = new String[5];
array_spinner[0] = "Name";
array_spinner[1] = "Address";
array_spinner[2] = "Phone";
array_spinner[3] = "Mobile";
array_spinner[4] = "Home";
Spinner s = (Spinner) findViewById(R.id.spin);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_spinner);
s.setAdapter(adapter);

}
}

<强> main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spin"
/>
</LinearLayout>