EditText对话框中的值

时间:2016-06-03 22:22:12

标签: java android

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

我收到错误:尝试调用虚方法' 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)

3 个答案:

答案 0 :(得分:1)

如果你想获得Dialog中的视图,那么你必须像这样膨胀布局 -

  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类型转换为EditText,这是不正确的。请检查您的xml并使用正确的视图ID。

答案 1 :(得分:0)

您需要从LayoutInflater获取对话框的视图,如下所示:

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

然后你可以打电话

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

答案 2 :(得分:0)

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

你的错误很清楚。

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