Android 旋转器问题
当我单击微调器时,我的应用程序崩溃并抛出异常,例如 “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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我面临同样的问题,现在已经解决了。我正在将 GroupActivity 用于基于选项卡的应用程序。
如果您正在使用 GroupActivity 并且出现此问题,那么这只是由于上下文所致。您无法将正确的上下文传递给微调器,并且会出现错误
现在解决方案是:
在 java 代码中动态创建微调器,如下所示,并将其添加到您的线性布局:
Spinner yourSpinner = new Spinner(getParent()); // 动态旋转器
yourLinearLayout.addView(yourDriverSpinner); // 你的线性布局
现在,用 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:
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
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.
:)
在类级别将其定义为成员变量
,将此行替换
为此
Define this at class level as member variable
replace this line
with this
这是简单的旋转器示例..
spinner.java
main.xml
here is simple spinner example..
spinner.java
main.xml