您的位置:首页 > 移动开发

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

2011-06-30 20:07 791 查看
这个错误是我在看Android SDK文档时,在一个Button的监听器里写了一个自定义的处理条,

代码如下:

//自定义处理条
dialog6.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();

LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
});


问题出在这句Context mContext = getApplicationContext();

应该写成Activity.this. Activity所在的Activity.

这是解释http://tech.shantanugoel.com/2010/07/08/badtokenexception-android-dialog-getapplicationcontext.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐