对话框中的AutoCompleteTextview ResourceNotFoundException

时间:2014-08-14 02:15:16

标签: android autocompletetextview

在对话框中创建AutoCompleteTextView时遇到问题

我创建视图的代码是

private void setPalleteOnClicks() {
    ImageButton newMethod = (ImageButton) findViewById(R.id.new_method);
    newMethod.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            final Dialog dialog = new Dialog(AndroidPDStoreActivity.this);
            dialog.setContentView(R.layout.method_creation);
            dialog.setTitle("New Method Creator");

            //String[] types = {"int", "double"};
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(AndroidPDStoreActivity.this,R.id.autoComplete, getResources().getStringArray(R.array.types));
            AutoCompleteTextView actv = (AutoCompleteTextView) dialog.findViewById(R.id.autoComplete);
            actv.setThreshold(1);
            actv.setAdapter(adapter);
            dialog.show();
        }

    });

}

我的Xml文件看起来像

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    .... 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Return Type: "
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <AutoCompleteTextView
            android:id="@+id/autoComplete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:dropDownHeight="100sp"
            android:text="" >

        </AutoCompleteTextView>

    </LinearLayout>

</LinearLayout>

我得到的例外是

08-14 14:11:14.524: E/AndroidRuntime(24167): FATAL EXCEPTION: main
08-14 14:11:14.524: E/AndroidRuntime(24167): Process: com.android.pdstore, PID: 24167
08-14 14:11:14.524: E/AndroidRuntime(24167): android.content.res.Resources$NotFoundException: Resource ID #0x7f050024 type #0x12 is not valid
08-14 14:11:14.524: E/AndroidRuntime(24167):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2314)
08-14 14:11:14.524: E/AndroidRuntime(24167):    at android.content.res.Resources.getLayout(Resources.java:939)
08-14 14:11:14.524: E/AndroidRuntime(24167):    at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
08-14 14:11:14.524: E/AndroidRuntime(24167):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
08-14 14:11:14.524: E/AndroidRuntime(24167):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
08-14 14:11:14.524: E/AndroidRuntime(24167):    at android.widget.AbsListView.obtainView(AbsListView.java:2255)
08-14 14:11:14.524: E/AndroidRuntime(24167):    at android.widget.ListPopupWindow$DropDownListView.obtainView(ListPopupWindow.java:1585)
08-14 14:11:14.524: E/AndroidRuntime(24167):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
08-14 14:11:14.524: E/AndroidRuntime(24167):    at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1167)
08-14 14:11:14.524: E/AndroidRuntime(24167):    at android.widget.ListPopupWindow.show(ListPopupWindow.java:554)
08-14 14:11:14.524: E/AndroidRuntime(24167):    at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1096)
08-14 14:11:14.524: E/AndroidRuntime(24167):    at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:971)
08-14 14:11:14.524: E/AndroidRuntime(24167):    at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:953) .....

我已尝试按照其他帖子的说明清理和删除R.java文件,但这没有帮助。只有在实际需要显示下拉菜单时才会出现此问题。

2 个答案:

答案 0 :(得分:1)

<强>问题:

new ArrayAdapter<String>(AndroidPDStoreActivity.this,R.id.autoComplete, getResources().getStringArray(R.array.types));

正如您可以在ArrayAdapter的构造函数中看到第二个参数,您正在xml中AutoCompleteTextView提供一个id Resources$NotFoundException。期望实例化视图的布局不是来自AutoCompleteTextView的id。

documentation

The resource ID for a layout file containing a layout to use when instantiating views.

<强>溶液

你需要调用AutoCompleteTextView的原生android布局来用作实例化视图的布局。

<强>样品:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(AndroidPDStoreActivity.this,
                        android.R.layout.simple_dropdown_item_1line,
                        getResources().getStringArray(R.array.types));

答案 1 :(得分:0)

这是与您拥有layout-[qualifier]个文件夹的多个屏幕尺寸相关的问题吗?您可能会遇到一个屏幕尺寸的资源,而另一个屏幕尺寸则没有。如果是,您可能希望将一些布局(根据屏幕大小不会更改的布局)移动到不合格的layout文件夹,而不是将其保留在特定的基于大小的文件夹中。