Prudhvi Reddy
Prudhvi Reddy

Reputation: 117

How to use AutoCompleteTextView in custom alert dialog?

I am trying to use an AutoCompleteTextView instead of EditText in my App. But the App is crashing with NullPointException .I am relatively new to android as well as java. So please help me declaring and using it.

My code-

Custom Alert XML Code-

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


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Enter Id"
    android:id="@+id/textView"
    android:textStyle="bold"
    android:textColor="#000000"
    android:layout_gravity="center_horizontal" />


<AutoCompleteTextView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/enterId"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

Alert Dialog code-

private static final String[] COUNTRIES = new String[] {
        "Belgium", "France", "Italy", "Germany", "Spain"};
protected void showInputDialog() {

    LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
    View promptView = layoutInflater.inflate(R.layout.originalalert, null);
    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
    alertDialogBuilder.setView(promptView);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, COUNTRIES);
    final AutoCompleteTextView textView = (AutoCompleteTextView)
            findViewById(R.id.enterId);
    textView.setAdapter(adapter);



    alertDialogBuilder.setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {



                    if (reload == 1) {
                        webView.reload();
                    }
                    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    mgr.hideSoftInputFromWindow(textView.getWindowToken(), 0);
                    myrollno = textView.getText().toString();

                }

            });
    alertDialogBuilder.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }

    });
    AlertDialog b = alertDialogBuilder.create();
    b.show();
}

Log file-

FATAL EXCEPTION: main
         Process: com.extremetechinnovators.prudhvi.bvritians, PID: 28455
         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.extremetechinnovators.prudhvi.bvritians/com.extremetechinnovators.prudhvi.bvritians.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.AutoCompleteTextView.setAdapter(android.widget.ListAdapter)' on a null object reference
             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
             at android.app.ActivityThread.access$900(ActivityThread.java:150)
             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
             at android.os.Handler.dispatchMessage(Handler.java:102)
             at android.os.Looper.loop(Looper.java:148)
             at android.app.ActivityThread.main(ActivityThread.java:5417)
             at java.lang.reflect.Method.invoke(Native Method)
             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.AutoCompleteTextView.setAdapter(android.widget.ListAdapter)' on a null object reference
             at com.extremetechinnovators.prudhvi.bvritians.MainActivity.showInputDialog(MainActivity.java:155)
             at com.extremetechinnovators.prudhvi.bvritians.MainActivity.onCreate(MainActivity.java:116)
             at android.app.Activity.performCreate(Activity.java:6285)
             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
             at android.app.ActivityThread.access$900(ActivityThread.java:150) 
             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
             at android.os.Handler.dispatchMessage(Handler.java:102) 
             at android.os.Looper.loop(Looper.java:148) 
             at android.app.ActivityThread.main(ActivityThread.java:5417) 
             at java.lang.reflect.Method.invoke(Native Method) 
             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Upvotes: 3

Views: 3161

Answers (2)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

NullPointerException

Thrown when an application attempts to use null in a case where an object is required .

You should pass your View Object promptView

AutoCompleteTextView textView =(AutoCompleteTextView)promptView.findViewById(R.id.enterId);

Upvotes: 3

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

Use below code to find the TextView which is inside your dialog.

AutoCompleteTextView textView = (AutoCompleteTextView)
            promptView.findViewById(R.id.enterId);

because with this

final AutoCompleteTextView textView = (AutoCompleteTextView)
            findViewById(R.id.enterId);

your is currently looking for R.id.enterId TextView in the current layout of activity

Upvotes: 5

Related Questions