选择Android微调器会导致WindowManager$BadTokenException

选择Android微调器会导致WindowManager$BadTokenException,android,exception,spinner,Android,Exception,Spinner,我创建了一个自定义AlertDialog来显示微调器和一些EditText元素。当我选择菜单选项时,将启动此自定义警报对话框。一切正常,直到我实际选择微调器来尝试并选择一个项目,在这一点上我得到了BadTokenException。我读过其他StackOverflow帖子,其中说类似的异常是由于尝试使用getApplicationContext()而不是传递Activity.this来显示diaglog造成的。我没有显式地将getApplicationContext()传递给与此AlertDia

我创建了一个自定义AlertDialog来显示微调器和一些EditText元素。当我选择菜单选项时,将启动此自定义警报对话框。一切正常,直到我实际选择微调器来尝试并选择一个项目,在这一点上我得到了BadTokenException。我读过其他StackOverflow帖子,其中说类似的异常是由于尝试使用getApplicationContext()而不是传递Activity.this来显示diaglog造成的。我没有显式地将getApplicationContext()传递给与此AlertDialog相关的任何内容

为了设置此自定义AlertDiaglog,我创建了一个包含微调器和EditText元素的布局文件,然后让AlertDialog使用该布局:

LayoutInflater inflater = (LayoutInflater)getApplication().getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_layout, (ViewGroup)findViewById(R.id.custom_layout_root));
spinner = (Spinner)layout.findViewById(R.id.custom_layout_spinner);
ArrayAdapter adap = ArrayAdapter.createFromResource(Activity.this, R.array.data, android.R.layout.simple_spinner_item);
adap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adap);
spinner.setOnItemSelectedListener(new SpinnerItemListener());

AlertDialog.Builder dialog = new AlertDialog.Builder(Activity.this);
dialog.setTitle("Title");
dialog.setPositiveButton("Ok", new Dialog.OnClickListener() ...
...
dialog.show();
这段代码可以很好地显示AlertDialog。但是,当我实际触摸微调器时,会出现以下问题:

Thread [<3> main] (Suspended (exception WindowManager$BadTokenException)) 
ViewRoot.handleMessage(Message) line: 1704
ViewRoot(Handler).dispatchMessage(Message) line: 99 
Looper.loop() line: 123 
ActivityThread.main(String[]) line: 4203
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 791
ZygoteInit.main(String[]) line: 549
NativeStart.main(String[]) line: not available [native method]
我相信这是其他人看到的关于这类问题的信息,所以我假设ApplicationContext在其中,但我不确定在哪里。我的清单是用1.5的minSdk和1.6的目标SDK设置的

我曾读过其他StackOverflow帖子,其中说类似的异常是由于试图使用getApplicationContext()而不是传递活动来显示diaglog造成的。这是。
我没有显式地将getApplicationContext()传递给与此AlertDialog相关的任何内容

假设这行代码实际上就是这样做的-您要求
应用程序使用其上下文来访问服务,而不是当前
窗口的上下文:

LayoutInflater inflater = 
  (LayoutInflater) getApplication().getSystemService(LAYOUT_INFLATER_SERVICE);
既然您正在从
活动
启动
警报对话框
,您就不能将其替换为以下版本吗

LayoutInflater inflater =
  (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
或者更简单:

flater充气机=

我曾读过其他StackOverflow帖子,其中说类似的异常是由于试图使用getApplicationContext()而不是传递活动来显示diaglog造成的。这是。
我没有显式地将getApplicationContext()传递给与此AlertDialog相关的任何内容

假设这行代码实际上就是这样做的-您要求
应用程序使用其上下文来访问服务,而不是当前
窗口的上下文:

LayoutInflater inflater = 
  (LayoutInflater) getApplication().getSystemService(LAYOUT_INFLATER_SERVICE);
既然您正在从
活动
启动
警报对话框
,您就不能将其替换为以下版本吗

LayoutInflater inflater =
  (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
或者更简单:


flater充气机=

我为这个奇怪的问题浪费了很多时间,但最终我解决了以下问题。请看下面的代码:

/**
 * Initializes the body of the screen that serves a user to edit a new question.
 */
private void onStart_initializeNewQuestion()
{
    /**
     * 0. Local variables.
     */
    Spinner spinner, spinner2;
    ArrayAdapter<CharSequence> arrayAdapter;
    EditText editText;

    /**
     * 1. Inflate the layout, and configure useful pointers.
     */
    this.viewGroup_body_newquestion= (ViewGroup) this.layoutInflater.inflate(R.layout.create_survey_newquestion, null);

    /**
     * 2. Populate the spinner with user choices, and then set responding spinner to user selections
     */
    spinner= (Spinner) this.viewGroup_body_newquestion.findViewById(R.id.create_survey_newquestion_spinner_questionType);

    // Create an ArrayAdapter using the string array and a default spinner layout
    arrayAdapter= ArrayAdapter.createFromResource(this, R.array.create_surveys_newquestion_spinnerOptions,
            android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(arrayAdapter);
    // spinner.setOnItemSelectedListener(this);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
        {
            String choice;
            choice= parent.getItemAtPosition(pos).toString();
            U.e(U.VERBOSE, "CreateSurveyActivity.onStart_initializeNewQuestion(listener@spinner_questionType): " + choice);

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0)
        {
            // TODO Auto-generated method stub

        }
    });

    spinner2= new Spinner(this);
    spinner2.setAdapter(arrayAdapter);
    spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
        {
            String choice;
            choice= parent.getItemAtPosition(pos).toString();
            U.e(U.VERBOSE, "CreateSurveyActivity.onStart_initializeNewQuestion(listener@spinner_questionType): " + choice);

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0)
        {
            // TODO Auto-generated method stub

        }
    });
    LinearLayout linearLayout= (LinearLayout) this.viewGroup_body_newquestion.getChildAt(0);
    linearLayout.addView(spinner2);


}

PS2:Christopher提供的解决方案允许继续使用原始微调器,因此这是一个更好的解决方案!:)这个奇怪的问题让我浪费了很多时间,但最终我解决了以下问题。请看下面的代码:

/**
 * Initializes the body of the screen that serves a user to edit a new question.
 */
private void onStart_initializeNewQuestion()
{
    /**
     * 0. Local variables.
     */
    Spinner spinner, spinner2;
    ArrayAdapter<CharSequence> arrayAdapter;
    EditText editText;

    /**
     * 1. Inflate the layout, and configure useful pointers.
     */
    this.viewGroup_body_newquestion= (ViewGroup) this.layoutInflater.inflate(R.layout.create_survey_newquestion, null);

    /**
     * 2. Populate the spinner with user choices, and then set responding spinner to user selections
     */
    spinner= (Spinner) this.viewGroup_body_newquestion.findViewById(R.id.create_survey_newquestion_spinner_questionType);

    // Create an ArrayAdapter using the string array and a default spinner layout
    arrayAdapter= ArrayAdapter.createFromResource(this, R.array.create_surveys_newquestion_spinnerOptions,
            android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(arrayAdapter);
    // spinner.setOnItemSelectedListener(this);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
        {
            String choice;
            choice= parent.getItemAtPosition(pos).toString();
            U.e(U.VERBOSE, "CreateSurveyActivity.onStart_initializeNewQuestion(listener@spinner_questionType): " + choice);

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0)
        {
            // TODO Auto-generated method stub

        }
    });

    spinner2= new Spinner(this);
    spinner2.setAdapter(arrayAdapter);
    spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
        {
            String choice;
            choice= parent.getItemAtPosition(pos).toString();
            U.e(U.VERBOSE, "CreateSurveyActivity.onStart_initializeNewQuestion(listener@spinner_questionType): " + choice);

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0)
        {
            // TODO Auto-generated method stub

        }
    });
    LinearLayout linearLayout= (LinearLayout) this.viewGroup_body_newquestion.getChildAt(0);
    linearLayout.addView(spinner2);


}

PS2:Christopher提供的解决方案允许继续使用原始微调器,因此这是一个更好的解决方案!:)

克里斯托弗,非常感谢!单独使用getLayoutInflater()就可以实现这一目的。从我读到的其他帖子中,我想我只是完全专注于传递给AlertDialog的内容,甚至没有注意Layoutiner。再次感谢!哇,谢谢你!这是可行的,但我不明白为什么除了新的Alertdialogs之外,其他一切都很好。克里斯托弗,非常感谢!单独使用getLayoutInflater()就可以实现这一目的。从我读到的其他帖子中,我想我只是完全专注于传递给AlertDialog的内容,甚至没有注意Layoutiner。再次感谢!哇,谢谢你!这是可行的,但我不明白为什么除了新的Alertdialogs之外,所有的东西都能正常工作。