在android中选择微调器时抛出错误

时间:2012-10-05 10:26:15

标签: android android-layout

我对android比较新。

当我点击手机上的微调器时,它会抛出此错误:

android.view.windowmanager$badtokenexception unable to add window --token null is not for an application

我的代码

List<String> list = new ArrayList<String>();
            list.add("Character");
            list.add("Numeric");
            list.add("Email");
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(CreateSteps.this,android.R.layout.simple_spinner_item, list);  

            LinearLayout spinnerHolder = new LinearLayout(getApplicationContext());
            spinnerHolder.setOrientation(LinearLayout.HORIZONTAL);
            spinnerHolder.setLayoutParams(
                    new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, 
                        LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
                final Spinner newSpinner = new Spinner(getApplicationContext());
                allSpns.add(newSpinner);
                newSpinner.setLayoutParams(
                    new Spinner.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
                        ViewGroup.LayoutParams.WRAP_CONTENT));
                newSpinner.setAdapter(dataAdapter);

                rAlign.addView(spinnerHolder);
                spinnerHolder.setId(MY_BUTTON);
                MY_BUTTON++;
                spinnerHolder.addView(newSpinner);

我尝试在运行android 2.3的手机上实现代码。

1 个答案:

答案 0 :(得分:1)

问题出在这里,

final Spinner newSpinner = new Spinner(getApplicationContext());

您需要传递Activity的上下文,而不是传递Application Context,

final Spinner newSpinner = new Spinner(ActivityName.this);