public void Home (View view){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Back");
dialog.setIcon(R.drawable.logo);
dialog.setCancelable(true);
dialog.setMessage("Do you want to go back?");
dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
});
dialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.show();
}
现在,当我单击“Home”按钮时,我停留在 pageOne.java 上。我想返回 MainActivity.java,但出现错误。
最佳答案
您对this
的使用有问题:
new Intent(this, MainActivity.class);
在该范围内,它认为 this
引用的是匿名内部类 DialogInterface.OnClickListener
。因此,您应该使用对外部类实例的引用,例如 HomeActivity.this
(或任何您的 Activity 的名称)。
new Intent(HomeActivity.this, MainActivity.class);
关于java - 如何 Intent Activity 使用AlertDialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34803381/