3

我对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 的手机上实现代码。

4

1 回答 1

1

问题就在这里,

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

您需要传递 Activity 的上下文,而不是传递应用程序上下文,

final Spinner newSpinner = new Spinner(ActivityName.this);
于 2012-10-05T10:27:32.313 回答