如何在自定义警报对话框中使用AutoCompleteTextView?

时间:2017-01-05 17:37:51

标签: android android-alertdialog

我正在尝试在我的应用中使用AutoCompleteTextView代替EditText。但是应用程序崩溃了NullPointException。我对android和java都比较新。所以请帮我宣布和使用它。

我的代码 -

自定义提醒XML代码 -

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

警报对话框代码 -

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();
}

日志文件 -

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) 

2 个答案:

答案 0 :(得分:5)

使用以下代码查找对话框中的TextView

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

因为有了这个

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

您目前正在寻找当前活动布局中的R.id.enterId TextView

答案 1 :(得分:3)

<强> NullPointerException

  

当应用程序在某个情况下尝试使用null时抛出   对象是必需的。

您应该传递View对象 promptView

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