我使用下面的代碼在 dialog 中設置 setView:
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.mylayout, null);
TextView MyTextView = (TextView)findViewById(R.id.my_tv);
MyTextView.setText("test");
AlertDialog MyDialog;
AlertDialog.Builder MyBuilder = new AlertDialog.Builder(this);
MyBuilder.setTitle("title");
MyBuilder.setView(layout);
MyDialog = MyBuilder.create();
MyDialog.show();
my_tv 在mylayout.xml中是一個TextView。
但是在MyTextView.setText("test");這一行有 NullPointer錯誤。
如何修改?
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.mylayout, null);
TextView MyTextView = (TextView)layout.findViewById(R.id.my_tv);//注意這一句
MyTextView.setText("test");
AlertDialog MyDialog;
AlertDialog.Builder MyBuilder = new AlertDialog.Builder(this);
MyBuilder.setTitle("title");
MyBuilder.setView(layout);
MyDialog = MyBuilder.create();
MyDialog.show();