无法在我的活动中显示微调器

发布于 2024-12-10 13:10:49 字数 3471 浏览 0 评论 0原文

我的儿童活动中有一个旋转器。这意味着我的活动是活动组的子活动,

当我单击微调器时,它崩溃了。在我单击选项之前它崩溃了(它甚至没有显示选项)请参阅此处:

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 技术交流群。

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

发布评论

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

评论(2

哀由 2024-12-17 13:10:49

尝试替换,

setContentView(R.layout.calculator);

通过

View contentView = LayoutInflater.from(getParent()).inflate(R.layout.calculator, null);
setContentView(contentView);

Try to replace,

setContentView(R.layout.calculator);

By

View contentView = LayoutInflater.from(getParent()).inflate(R.layout.calculator, null);
setContentView(contentView);
凉城已无爱 2024-12-17 13:10:49

尝试将适配器的Context更改为当前的活动上下文。该行:

ArrayAdapter<String> metalDataAdapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Metals));

应该更改为:

ArrayAdapter<String> metalDataAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Metals));

AFAIK 在创建所有 UI 组件时最好使用当前的 Activity 上下文

Try changing the Context of you adapter to your current Activity Context. That line:

ArrayAdapter<String> metalDataAdapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Metals));

should be changed to:

ArrayAdapter<String> metalDataAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Metals));

AFAIK it's better to use current Activity context in creating all UI components

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