编辑对话框中的文本值



我不明白,对话框和一切正常,但是一旦我从 EditText 获取值并将其分配给 val,运行该应用程序就会在 onClick 成功时崩溃。

我收到一个错误:尝试在空对象引用上调用虚拟方法"android.text.Editable android.widget.EditText.getText()"

public void createDialogBox() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    // Get the layout inflater
    LayoutInflater inflater = this.getLayoutInflater();
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    //

    builder.setView(R.layout.custom_dialog)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // On success
                    EditText text = (EditText) findViewById(R.id.shopping_list_recycler_view);
                    String val = text.getText().toString();
                    adapter = new ShoppingListViewAdapter(test, MainShoppingListViewActivity.this);
                    recyclerView.setAdapter(adapter);
                    adapter.addTest(new Data(val, "Created by: Me"));
                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // On failure
                    Toast.makeText(MainShoppingListViewActivity.this, "Failed", Toast.LENGTH_SHORT).show();
                }
            })
            // Desired icon for the dialog box
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();
}

原木

06-03 22:37:29.778 21364-21364/familyshopshare.com.familyshopshare E/AndroidRuntime: FATAL EXCEPTION: main
Process: familyshopshare.com.familyshopshare, PID: 21364
java.lang.ClassCastException: android.support.v7.widget.RecyclerView cannot be cast to android.widget.EditText
 at familyshopshare.com.familyshopshare.MainShoppingListViewActivity$3.onClick(MainShoppingListViewActivity.java:93)
 at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
 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)
如果你想

在对话框中获取视图,那么你必须像这样膨胀布局 -

  View rootView = inflater.inflate(R.layout.custom_dialog, null);

然后初始化您的视图 -

final EditText text = (EditText)rootView.findViewById(R.id.shopping_list_recycler_view);

并在对话框中将其设置为视图-

builder.setView(rootView);

然后你可以在 onClick() 中访问你的 EditText,就像这样 -

public void onClick(DialogInterface dialog, int which) {
         // On success 
         String val = text.getText().toString();
}

从崩溃中,很明显您错误地转换了视图 ID。您正在将shopping_list_recycler_view类型转换为不正确的编辑文本。请检查您的 xml 并使用正确的视图 ID。

您需要

LayoutInflater获取对话框的视图,如下所示:

View rootView = layoutInflater.inflate(R.layout.dialog_layout, null);

然后你可以打电话

EditText text = (EditText) rootView.findViewById(R.id.shopping_list_recycler_view);

请检查您的 XML 以获取正确的 ID 作为您的EditText

你的错误很清楚。

RecyclerView cannot be cast to android.widget.EditText

您还像以前一样使用findViewById在活动中搜索,因此您需要从对话框的视图中获取它。

// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
//
View dialogView = getLayoutInflate().inflate(R.layout.custom_dialog, null);
// Get your Views
final EditText editText = dialogView.findViewById(R.id.someEditText); 
// Build and show the dialog
builder.setView(dialogView)
       ...
       .create().show();

最新更新