android.view.WindowManager$BadTokenException: Unable to add window -- token

最近遇到 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
具體爲什麼,我現在也還不理解。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章