自定义对话框中的getText

时间:2016-08-23 05:39:24

标签: android android-alertdialog gettext

我一直在尝试这样做,以便我可以将用户输入的任何数据保存到自定义对话框的EditText中,但没有成功。该应用程序每次都会在具有getText()功能的行上崩溃。任何帮助将深表感谢。

public void comments(View v){

    AlertDialog.Builder builder2;

    builder2 = new AlertDialog.Builder(this);
    // Get the layout inflater
    LayoutInflater inflater = getLayoutInflater();

    builder2.setTitle("Recipe Comments");
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    final View view = inflater.inflate(R.layout.fragment_comments, null);

    builder2.setView(view)
            // Add action buttons
            .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // sign in the user ...
                    EditText text = (EditText)view.findViewById(R.id.comments);
                    String temp = text.getText().toString();
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //LoginDialogFragment.this.getDialog().cancel();
                }
            });
    builder2.create();
    builder2.show();
}

这是StackTraces:

  

08-22 23:43:38.111 10837-10897 / com.example.wesle.wsuuioption1   I / OpenGLRenderer:初始化的EGL,版本1.4       08-22 23:43:43.568 10837-10837 / com.example.wesle.wsuuioption1 D / AndroidRuntime:关闭VM       08-22 23:43:43.571 10837-10837 / com.example.wesle.wsuuioption1 E / AndroidRuntime:FATAL EXCEPTION:main                                                                                       处理:com.example.wesle.wsuuioption1,PID:10837                                                                                       java.lang.NullPointerException:尝试调用虚方法   'android.text.Editable android.widget.EditText.getText()'上的null   对象参考                                                                                           在   com.example.wesle.wsuuioption1.MainActivity $ 7.onClick(MainActivity.java:2936)                                                                                           在   android.support.v7.app.AlertController $ ButtonHandler.handleMessage(AlertController.java:157)                                                                                           在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                           在android.os.Looper.loop(Looper.java:148)                                                                                           在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                           at java.lang.reflect.Method.invoke(Native Method)                                                                                           在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2 个答案:

答案 0 :(得分:0)

在设置视图之前建立连接,并将其设为最终。

public void comments(View v){

    AlertDialog.Builder builder2;

    builder2 = new AlertDialog.Builder(this);
    // Get the layout inflater
    LayoutInflater inflater = getLayoutInflater();

    builder2.setTitle("Recipe Comments");
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    final View view = inflater.inflate(R.layout.fragment_comments, null);

    // Make connection her before setting view.
    final EditText text = (EditText)view.findViewById(R.id.comments);

    builder2.setView(view)
            // Add action buttons
            .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // sign in the user ...
                    //EditText text = (EditText)view.findViewById(R.id.comments);
                    String temp = text.getText().toString();
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //LoginDialogFragment.this.getDialog().cancel();
                }
            });
    builder2.create();
    builder2.show();
}

答案 1 :(得分:0)

您无法通过views获取组件。只需从DialogInterface object

获取
    public void comments(View v){

    AlertDialog.Builder builder2;

    builder2 = new AlertDialog.Builder(this);
    // Get the layout inflater
    LayoutInflater inflater = getLayoutInflater();

    builder2.setTitle("Recipe Comments");
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    final View view = inflater.inflate(R.layout.fragment_comments, null);


    builder2.setView(view)
            // Add action buttons
            .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // sign in the user ...
                    EditText text = (EditText)dialog.findViewById(R.id.comments);
                    String temp = text.getText().toString();
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //LoginDialogFragment.this.getDialog().cancel();
                }
            });
    builder2.create();
    builder2.show();
}