Search code examples
javaandroidautocompletetextviewillegalstateexception

could not execute method of the activity: AutoCompleteTextView.setAdapter


I am trying to display a dialog from an application which has an AutoCompleteTextView.

Getting the error while trying to set the adapter.

from 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" />

from 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>

Error Log

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

Solution

  • You need to specify the view that you are calling findViewById(...) on. Otherwise it will try to use the layout that you set for your Activity, and that will cause NullPointerException since it isn't in that layout.

    Example:

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