无法执行活动的方法:AutoCompleteTextView.setAdapter

时间:2015-08-07 13:24:13

标签: java android autocompletetextview illegalstateexception

我正在尝试从具有AutoCompleteTextView的应用程序中显示一个对话框。

尝试设置适配器时出错。

来自activity_main.xml

    <Button
        android:id="@+id/lvftr_btn_shownew"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:text="@string/str_lvftr_addnew"
        android:onClick="showNewDialog" />
来自MainActivity.java的

public void showNewDialog(View view){
    Dialog newDlg = new Dialog(this);
    newDlg.setContentView(R.layout.new_dlg);

    String[] catStrArr = getResources().getStringArray(R.array.cat_string); //getting the string array from strings.xml
    ArrayList<String> categoryList       = new ArrayList<String>(Arrays.asList(catStrArr));
    ArrayAdapter<String> categoryAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, categoryList);
    AutoCompleteTextView categoryText    = (AutoCompleteTextView) findViewById(R.id.new_category);
    categoryText.setAdapter(categoryAdapter); //getting the error from this line.

    newDlg.show();
}

new_dlg.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" >

<AutoCompleteTextView
    android:id="@+id/new_category"
    android:layout_width="99dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.12"
    android:ems="10"
    android:hint="@string/str_new_category" >
    <requestFocus />
</AutoCompleteTextView>

</LinearLayout>

错误记录

FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
    at android.view.View$1.onClick(View.java:3597)
    at android.view.View.performClick(View.java:4202)
    at android.view.View$PerformClick.run(View.java:17340)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5039)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at android.view.View$1.onClick(View.java:3592)
    ... 11 more
Caused by: java.lang.NullPointerException
    at com.example.tabtest.MainActivity.showNewDialog(MainActivity.java:118)
    ... 14 more

1 个答案:

答案 0 :(得分:0)

您需要指定您正在呼叫view的{​​{1}}。否则,它会尝试使用您为findViewById(...)设置的布局,这将导致Activity,因为它不在该布局中。

示例:

NullPointerException