无法在我的活动中显示微调器
我的儿童活动中有一个旋转器。这意味着我的活动是活动组的子活动,
当我单击微调器时,它崩溃了。在我单击选项之前它崩溃了(它甚至没有显示选项)请参阅此处:
public class PriceCalculator extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
Spinner metalSpinner=(Spinner)findViewById(R.id.metalSpinner);
ArrayAdapter<String> metalDataAdapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Metals));
metalSpinner.setAdapter(metalDataAdapter);
metalSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
TextView priceTextView=(TextView)findViewById(R.id.priceTextView);
priceTextView.setText(""+parent.getItemAtPosition(position));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
}
我的错误日志此处:
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): FATAL EXCEPTION: main
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44eed660 is not valid; is your activity running?
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.view.ViewRoot.setView(ViewRoot.java:505)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.app.Dialog.show(Dialog.java:241)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.widget.Spinner.performClick(Spinner.java:257)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.view.View$PerformClick.run(View.java:8816)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.os.Handler.handleCallback(Handler.java:587)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.os.Handler.dispatchMessage(Handler.java:92)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.os.Looper.loop(Looper.java:123)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at java.lang.reflect.Method.invoke(Method.java:521)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at dalvik.system.NativeStart.main(Native Method)
您的活动正在运行吗? 但我的活动正在运行。难道是孩子的活动发生了?
I have a spinner in my Child activity. That means my activity is child of a Activity Group
When I click on my spinner it crashed. It crashes before I'm clicking a choice (It didn't even show the choices) see here:
public class PriceCalculator extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
Spinner metalSpinner=(Spinner)findViewById(R.id.metalSpinner);
ArrayAdapter<String> metalDataAdapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Metals));
metalSpinner.setAdapter(metalDataAdapter);
metalSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
TextView priceTextView=(TextView)findViewById(R.id.priceTextView);
priceTextView.setText(""+parent.getItemAtPosition(position));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
}
My error log here:
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): FATAL EXCEPTION: main
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44eed660 is not valid; is your activity running?
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.view.ViewRoot.setView(ViewRoot.java:505)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.app.Dialog.show(Dialog.java:241)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.widget.Spinner.performClick(Spinner.java:257)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.view.View$PerformClick.run(View.java:8816)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.os.Handler.handleCallback(Handler.java:587)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.os.Handler.dispatchMessage(Handler.java:92)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.os.Looper.loop(Looper.java:123)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at java.lang.reflect.Method.invoke(Method.java:521)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-18 17:25:24.828: ERROR/AndroidRuntime(32670): at dalvik.system.NativeStart.main(Native Method)
is your activity running?
But my activity is running. Is that happens the activity is a child ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试替换,
通过
Try to replace,
By
尝试将适配器的
Context
更改为当前的活动上下文。该行:应该更改为:
AFAIK 在创建所有 UI 组件时最好使用当前的 Activity 上下文
Try changing the
Context
of you adapter to your current Activity Context. That line:should be changed to:
AFAIK it's better to use current Activity context in creating all UI components