Pru*_*ddy 2 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" />
Run Code Online (Sandbox Code Playgroud)
警报对话框代码-
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();
}
Run Code Online (Sandbox Code Playgroud)
日志文件
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)
Run Code Online (Sandbox Code Playgroud)
使用以下代码查找TextView对话框内部的。
AutoCompleteTextView textView = (AutoCompleteTextView)
promptView.findViewById(R.id.enterId);
Run Code Online (Sandbox Code Playgroud)
因为有了这个
final AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.enterId);
Run Code Online (Sandbox Code Playgroud)
您当前正在R.id.enterId TextView当前活动布局中寻找
| 归档时间: |
|
| 查看次数: |
1628 次 |
| 最近记录: |