Android 旋转器问题

发布于 2024-12-04 14:18:42 字数 3274 浏览 0 评论 0原文

当我单击微调器时,我的应用程序崩溃并抛出异常,例如 “android.view.WindowManager$BadTokenException:无法添加窗口 - token null 不适用于应用程序”

 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
        }
    }

在此处输入代码 日志猫

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}

when i click on spinner my application crashes ans it throws an exception like "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}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

歌枕肩 2024-12-11 14:18:42

我面临同样的问题,现在已经解决了。我正在将 GroupActivity 用于基于选项卡的应用程序。

如果您正在使用 GroupActivity 并且出现此问题,那么这只是由于上下文所致。您无法将正确的上下文传递给微调器,并且会出现错误

现在解决方案是:

  1. 创建任何布局(如 LinearLayout)来代替 xml 中的微调器
  2. 在 java 代码中动态创建微调器,如下所示,并将其添加到您的线性布局:

    Spinner yourSpinner = new Spinner(getParent()); // 动态旋转器
    yourLinearLayout.addView(yourDriverSpinner); // 你的线性布局

  3. 现在,用 spinner 做任何你想做的事。

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

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

享受。

:)

I am facing same issue and now have solved it. I am using GroupActivity for Tab based application.

If you are using GroupActivity and if this problem occurs then it is just because of the context. You are not able to pass the right context to the spinner and it gives error

Now the Solution is:

  1. Create any layout like LinearLayout in place of the spinner in your xml
  2. Create Spinner dynamically at java code like below and add that to your linearLayout:

    Spinner yourSpinner = new Spinner(getParent()); // dynamically Spinner
    yourLinearLayout.addView(yourDriverSpinner); // your linearlayout

  3. Now, do whatever you want to do with spinner.

This help me a lot and will also help you.

Still if there is any error then let me know.

Enjoy.

:)

魂ガ小子 2024-12-11 14:18:42

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

String[] s = new String[60];

,将此行替换

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

为此

spvalue = s[pos];

Define this at class level as member variable

String[] s = new String[60];

replace this line

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

with this

spvalue = s[pos];
泪冰清 2024-12-11 14:18:42

这是简单的旋转器示例..

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);

}
}

ma​​in.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>

here is simple spinner example..

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文