我创建了一个自定义对话框,但是我无法将文本设置为java中对话框布局中的textview,程序崩溃了。我的错是什么?
public class Total_CBC extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.total_cbc);
final TextView RBC_T = (TextView) findViewById(R.id.total_cbc_text_rbc);
Button RBC_B = (Button) findViewById(R.id.total_cbc_btn_rbc);
//
RBC_B.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
showCustomDialog(RBC_T);
}
});
}
protected void showCustomDialog(final TextView _RBC_T) {
final Dialog dialog = new Dialog(Total_CBC.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_edit);
final EditText editText = (EditText) dialog.findViewById(R.id.dialog_edit_edittext);
Button button = (Button) dialog.findViewById(R.id.dialog_edit_btn);
TextView titel = (TextView) findViewById(R.id.dialog_edit_text_title);
titel.setText("RBC");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
_RBC_T.setText(editText.getText().toString());
dialog.dismiss();
}
});
dialog.show();
}
}当我删除此行程序时,我需要设置文本
titel.setText("RBC");
答案 0 :(得分:3)
更改
TextView titel = (TextView) findViewById(R.id.dialog_edit_text_title);
到
TextView titel = (TextView) dialog.findViewById(R.id.dialog_edit_text_title);
答案 1 :(得分:0)
您正在尝试在显示对话框之前设置TextView的文本,这就是为什么它返回null。 正确的方法是在创建对话框后设置文本:
将其放入自定义对话框
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
TextView titel = (TextView)dialog.findViewById(R.id.dialog_edit_text_title);
titel.setText("RBC");
return builder.create();
}