Android 从自定义对话框获取文本

Android 从自定义对话框获取文本,android,android-alertdialog,gettext,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

我一直在尝试将用户输入的任何数据保存到自定义对话框的
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();
}
以下是我的建议:

08-22 23:43:38.111 10837-10897/com.example.wesle.wsuioption 1 I/OpenGLRenderer:初始化EGL,版本1.4 08-22 23:43:43.568 10837-10837/com.example.wesle.wsuuioption1 D/AndroidRuntime:关闭虚拟机 08-22 23:43:43.571 10837-10837/com.example.wesle.wsuuioption1 E/AndroidRuntime:致命异常:main 进程:com.example.wesle.wsuuioption1,PID:10837 java.lang.NullPointerException:尝试调用虚拟方法 空值上的“android.text.Editable android.widget.EditText.getText()” 对象引用 在 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) 位于java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


在设置视图之前连接她,并使其成为最终视图

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

您无法通过
视图
获取组件。只需从
对话框界面
对象

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

发布你的日志,这样人们就可以发现你犯了什么错误……刚刚发布,谢谢。请检查你的
R.layout.fragment\u comments
edittext的id是
comments
,或者发布你的xml文件是你的
R.id.comments
属于
R.layout.fragment\u comments
,修复了它。谢谢那零钱还砸在我身上