gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:50:43 25 4
gpt4 key购买 nike

我正在尝试在我的应用程序中使用 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)

最佳答案

使用下面的代码找到对话框中的 TextView

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

因为有了这个

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

您当前正在寻找 R.id.enterId TextView 在当前 Activity 布局中

关于android - 如何在自定义警报对话框中使用 AutoCompleteTextView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41491328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com