无法执行 activity 的方法:AutoCompleteTextView.setAdapter

could not execute method of the activity: AutoCompleteTextView.setAdapter

我正在尝试显示来自具有 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.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.onClick(View.java:3592)
    ... 11 more
Caused by: java.lang.NullPointerException
    at com.example.tabtest.MainActivity.showNewDialog(MainActivity.java:118)
    ... 14 more

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

示例:

AutoCompleteTextView categoryText = (AutoCompleteTextView) view.findViewById(R.id.new_category);