本文介绍了关于活动的再创造解释的配置变化和环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新Android开发,我在寻找有关我面临一个问题的解释,对于获得的Andr​​oid更深的理解。

I'm pretty new to Android development and I'm looking for explanation about a problem I'm facing, for gaining deeper understanding of Android.

我有这块code的:

someAutoCompleteTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus)
            ((AutoCompleteTextView)v).showDropDown();
        else
            ((AutoCompleteTextView)v).dismissDropDown();
    }       
});

如果下拉列表是可见的配置更改(屏幕方向)我得到一个BadTokenException。

if the dropdown list is visible and the configuration changes (screen orientation) I'm getting a BadTokenException.

据我了解,该活动被破坏,一个新的创建来取代它,但我不明白到底发生了什么以及为什么我会收到这个异常,
毕竟,一个新的活动与注册新观点新的听众和旧的被破坏创建。

I understand that the activity is destroyed and a new one is created to replace it, but I don't quite understand what's really going on and why am I getting that exception,after all, a new activity is created with new listeners registered to the new views and the old ones are destroyed.

我知道,我可以告诉,我会用我自己来处理配置更改舱单解决这个问题,但我寻找到更深入的了解。

I know that I can fix this by telling the manifest that I'll be handling configuration changes by myself, but I'm looking into deeper understanding.

谢谢!

推荐答案

我想,当你在横向模式,而当AutoCompleteTextView集中,这可编辑的字段切换到全屏模式,即只值和键盘显示在屏幕上。

I suppose that when you are in landscape mode, and when the AutoCompleteTextView is focused, this editable field is switching to "Full Screen Mode", i.e. only the value and the keyboard are shown on screen.

所以我在这种情况下猜下拉是不可见的,这就是为什么showDropDown()抛出异常。

So I guess in this case the drop down is never visible and that is why showDropDown() throws an exception.

要避免这种情况,加上下面几行中的code:

To avoid this, add the following lines in your code:

someAutoCompleteTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
    public void onFocusChange(View v, boolean hasFocus) {

        if (v.getWindowVisibility() != View.VISIBLE) {
            return;
        }

        if (hasFocus)
            ((AutoCompleteTextView)v).showDropDown();
        else
            ((AutoCompleteTextView)v).dismissDropDown();
    }       
});

这篇关于关于活动的再创造解释的配置变化和环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:40