Android 无法在通过活动的编辑文本输入的自定义对话框的文本视图中设置文本

Android 无法在通过活动的编辑文本输入的自定义对话框的文本视图中设置文本,android,Android,在主活动中,我通过EditText输入一些文本,然后单击SUBMIT按钮,该按钮调用openDialog()方法。openDialog()方法正在为我构建一个自定义对话框。在此自定义对话框中,我希望在id为lblName的TextView中显示通过id为edtName的EditText输入的文本。设置值时,我得到了NullPointerException。根据你的问题,我发现TextView textName=(TextView)view.findViewById(R.id.lblName)是布

在主活动中,我通过EditText输入一些文本,然后单击SUBMIT按钮,该按钮调用openDialog()方法。openDialog()方法正在为我构建一个自定义对话框。在此自定义对话框中,我希望在id为lblName的TextView中显示通过id为edtName的EditText输入的文本。设置值时,我得到了NullPointerException。

根据你的问题,我发现
TextView textName=(TextView)view.findViewById(R.id.lblName)是布局属性,所以称之为对话框部分

基本上你的logcat

空指针异常 当你的程序试图使用null,就好像它是一个真正的引用一样,在运行时抛出。 这样试试看

public class MainActivity extends Activity {
EditText user;
String name;
AlertDialog.Builder dialogBuilder;
TextView textName;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    user = (EditText) findViewById(R.id.edtName);
    textName = (TextView) findViewById(R.id.lblName);
}

public void openDialog(View v) {
    dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.custom_layout, null, false);
    dialogBuilder.setTitle(" Custom Dialog");
    dialogBuilder.setView(view);
    AlertDialog dialog = dialogBuilder.create();
    dialog.show();
    name = user.getText().toString();
    textName.setText(name);
   }
}
我希望它能帮助您在自定义布局中使用大量的方法。在自定义布局中,您应该有文本视图,这里是textName,它是自定义
布局(R.layout.custom_布局)
的一部分,而不是活动
布局(即R.layout.activity_main)

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        user = (EditText) findViewById(R.id.edtName);

    }   



    public void openDialog(View v) {

       dialogBuilder = new AlertDialog.Builder(this);
       LayoutInflater inflater = (LayoutInflater)    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       View view = inflater.inflate(R.layout.custom_layout, null, false);

       TextView anotherView = (TextView)view.findViewById( textViewId); 
       String name = user.getText().toString();
       anotherView.setText(name);

       dialogBuilder.setTitle(" Custom Dialog");
       dialogBuilder.setView(view);
       AlertDialog dialog = dialogBuilder.create();
       dialog.show();
    }
 }
因此,您必须在openDialog()方法中编写类似的内容


但在这里,您要在活动视图而不是自定义对话框上的textName textview上设置名称。如果您想在自定义对话框中显示文本,请在自定义对话框中添加一个TextView并在其中设置名称(该名称取自id为edtName的EditText)@techroid是的是的,您很完美
public void openDialog(View v) {
    dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.custom_layout, null, false);

    TextView anotherView = (TextView)view.findViewById( textViewId); //enter resource id
    String name = user.getText().toString();
    anotherView.setText(name);

    dialogBuilder.setTitle(" Custom Dialog");
    dialogBuilder.setView(view);
    AlertDialog dialog = dialogBuilder.create();
    dialog.show();

   }
    View view = inflater.inflate(R.layout.custom_layout, null, false);
    // get textName reference
    textName = (TextView) view.findViewById(R.id.lblName);
    dialogBuilder.setTitle(" Custom Dialog");
    dialogBuilder.setView(view);
    AlertDialog dialog = dialogBuilder.create();
    name = user.getText().toString();
    textName.setText(name);
    dialog.show();