android debug:Unable to add window -- token null is not for an application

Unable to add window -- token null is not for an application

<span style="font-size:14px;">listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				final int pos = position;
				AlertDialog.Builder builder =  new AlertDialog.Builder(FriendActivity.this);//这里不能用getAppliacationContext
				builder.setTitle("提示").setMessage("您确定要删除吗?");
				builder.setPositiveButton("确定", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						friendAdapter.remove(friendAdapter.getItem(pos));
						dialog.dismiss();
					}
				});
				builder.setNegativeButton("取消", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				builder.create().show();
			}
				
		});</span>

错误产生的地方在代码中注释处,使用getApplicationContext会报错。

导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)不能使用getApplicationContext()获得的Context,而必须使用Activity,因为只有一个Activity才能添加一个窗体。 

你可能感兴趣的:(Android开发)