Build a dialog I'd like add a listener, but the app crashes.What is wrong?
private void Info(){
textview = (TextView) findViewById(R.id.textView1);
LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.info, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view).create().show();
buttonInfo = (Button)findViewById(R.id.buttonInfo);
buttonInfo.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
});
replace
buttonInfo = (Button) view.findViewById(R.id.buttonInfo);
by
buttonInfo = (Button) findViewById(R.id.buttonInfo);
final code
private void Info(){
textview = (TextView) findViewById(R.id.textView1);
LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.info, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view).create().show();
buttonInfo = (Button) view.findViewById(R.id.buttonInfo);
buttonInfo.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
});
I guess that the button is inside your dialog's layout.
If it's the case, try to replace
buttonInfo = (Button)findViewById(R.id.buttonInfo);
by
buttonInfo = (Button) view.findViewById(R.id.buttonInfo);
And you need to store the created AlertDialog (instead of the Builder) if you want to dismiss it after:
final AlertDialog dialog = new AlertDialog.Builder(this).setView(view).show();
buttonInfo = (Button) findViewById(R.id.buttonInfo);
buttonInfo.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
dialog.dismiss();
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.