最近遇到 android logcat 报这样的错误.
android.view.WindowManager$BadTokenException: Unable to add window -- token
AlertDialog.Builder a = new AlertDialog.Builder(context);
a.setTitle("购物清单")
.setIcon(R.drawable.shopping)
.setView(tv)
.setNegativeButton("取消", null)
.setPositiveButton("确认购买", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(context,ItemActivity.class);
startActivity(intent);
}
});
Dialog dialog = a.show();
原因就出在context上面。
这里的context 需要换成你的 类名,比如 MainActivity.this
具体为什么,我现在也还不理解。