常用上下文getApplicationContext()-this-getContext()
弹出对话框 AlertDialog.Builder builder = new Builder(getApplicationContext()); 使用上面的getApplicationContext()会报下面的异常,但如果用this,就没事了。 10-26 12:23:14.944: E/AndroidRuntime(7882): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application public void click(View view) {
// AlertDialog.Builder builder = new Builder(this);
Toast.makeText(getApplicationContext(), "呵呵", 0).show();
// AlertDialog.Builder builder = new Builder(getApplicationContext());
//10-26 12:51:57.400: E/AndroidRuntime(1755): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
// 对话框是activity的一部分 弹出对话框 不会执行onpasue方法
// 对话框在创建的时候 必须要指定对话框挂载在哪个activity上.
AlertDialog.Builder builder = new Builder(this);
// this activity上下文的子类
builder.setTitle("提醒!");
builder.setMessage("您确定下载吗?");
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("确定。。。");
}
});
builder.show();
} |
||
|