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

时间:2011-10-18 12:06:22

标签: java android spinner

我的儿童活动中有一个微调器。这意味着我的活动是活动组的孩子

当我点击我的旋转器时,它崩溃了。它在我点击一个选项之前崩溃(它甚至没有显示选择),请看这里:

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)

您的活动在运行吗? 但我的活动正在进行中。这是否发生了活动是一个孩子?

2 个答案:

答案 0 :(得分:2)

尝试替换,

setContentView(R.layout.calculator);

通过

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

答案 1 :(得分:0)

尝试将适配器的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上下文