2

当我旋转屏幕时,我在 Android 的回调中执行了几个代码实例。由于这种“错误”行为,我总是测试屏幕旋转。但是今天我想也许我做错了什么,或者这是一个应该报告给Android项目的错误。

最新的例子是这个

我有一个自动完成字段。

FindReplaceAutoCompleteAdapter mAutoCompleteAdapter;
protected void onCreate(...){
    mAutoCompleteAdapter = new FindReplaceAutoCompleteAdapter(this,
            android.R.layout.simple_dropdown_item_1line);

    mFindRule.setAdapter(mAutoCompleteAdapter);
}

我有适配器

public class FindReplaceAutoCompleteAdapter extends
        ArrayAdapter<FindReplaceRuleContainer> {

    private FindReplaceRuleFilter mFilter = null;

    @Override
    public Filter getFilter() {

        if (mFilter == null)
            mFilter = new FindReplaceRuleFilter();
        return mFilter;
    }

    public FindReplaceAutoCompleteAdapter(Context context,
            int textViewResourceId) {
        super(context, textViewResourceId);
    }

}

和过滤器

public class FindReplaceRuleFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults fr = null;
        if (constraint != null) {
            List<FindReplaceRuleContainer> list = getFilterdValues(constraint
                    .toString());

            if (list != null) {
                fr = new FilterResults();
                fr.values = list;
                fr.count = list.size();
            }
        }
        return fr;
    }

    private List<FindReplaceRuleContainer> getFilterdValues(String string) {
        List<FindReplaceRuleContainer> lst = Sdbi
                .loadFindReplaceRulesAutoComplete(FindReplaceList.this,
                        string);
        return lst;
    }

    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        /**
         * Publish the result to the list activity so it looks pretty.
                     * Instead of a drop-down list
         */
        mAutoCompleteAdapter.clear();
        if( Adapter != null ){ //this is the fix I had to do because 
                     //this code is called even though no filter action was launched
                     //It crashes because the Adapter is reloaded on an AsyncTask that
                     //start onResume() and may not be done by the time this code
                     //inexplicably resides to run
            Adapter.clear();
            if (results != null) { 
            ...
            }
            Adapter.notifyDataSetChanged();
        }
    }

}

当我在 onResume() 之后旋转屏幕时,会调用 publishResults() 中的代码。但是当屏幕旋转时,甚至没有触摸 AutoComplete 字段。

在其他 UI 组件上可以看到此行为,但我未能记录其他实例。

4

0 回答 0