无法在通过活动的EditText输入的自定义对话框的TextView中设置文本

时间:2015-08-10 12:34:12

标签: android

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

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

3 个答案:

答案 0 :(得分:1)

根据您的问题,我的事物TextView textName = (TextView)view.findViewById(R.id.lblName);Dialog布局属性,因此将其称为对话框部分。

基本上你的logcat抛出

的NullPointerException

只要你的程序试图使用null就好像它是一个真正的引用,就会在runtime抛出

。在这里你的TextView面对这个。 试试这种方式,

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

我希望它对你有所帮助

答案 1 :(得分:1)

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

   }

答案 2 :(得分:0)

openDialog()方法中,您正在为Dialog扩展自定义布局。在自定义布局中,您应该有文本视图,这里是 textName ,它是您自定义的一部分< strong> layout(R.layout.custom_layout) 不是您的活动 layout(i.e. R.layout.activity_main)

所以你必须在openDialog()方法中写这样的东西。

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