Android WindowManager显示自定义对话框时出现异常

Android WindowManager显示自定义对话框时出现异常,android,Android,当我运行代码时,出现以下错误: : E/InputEventReceiver(1363): Exception dispatching input event. : E/MessageQueue-JNI(1363): Exception in MessageQueue callback: handleReceiveCallback : D/dalvikvm(1363): GC_CONCURRENT freed 1898K, 30% free 4921K/6992K, paused

当我运行代码时,出现以下错误:

  : E/InputEventReceiver(1363): Exception dispatching input event.

  : E/MessageQueue-JNI(1363): Exception in MessageQueue callback: handleReceiveCallback

  : D/dalvikvm(1363): GC_CONCURRENT freed 1898K, 30% free 4921K/6992K, paused 78ms+107ms, total 413ms

  : E/MessageQueue-JNI(1363):  android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for  an application

  : E/MessageQueue-JNI(1363):at  com.example.ikmantest2.MainActivity$6.onItemClick(MainActivity.java:269)
这是我得到的错误代码:

gallery.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            final Dialog dialog = new Dialog(getBaseContext());
            dialog.setContentView(R.layout.image_dialog_layout);


            // set the custom dialog components - text, image and button
            imageView=(ImageView)dialog.findViewById(R.id.bigger_image);
            imageView.setImageBitmap(exListAdapter.getImageByPosition(lastClickedGroup, arg2));
            pre=(ImageButton)dialog.findViewById(R.id.btn_pre);
            back=(ImageButton)dialog.findViewById(R.id.btn_back);
            next=(ImageButton)dialog.findViewById(R.id.btn_next); 

            // if button is clicked, close the custom dialog
            pre.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            back.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            next.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            /*this is line number 269 as in error code*/  dialog.show();
        }

    }); 
gallery.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0、视图arg1、内部arg2、,
长arg3){
最终对话框=新对话框(getBaseContext());
setContentView(R.layout.image_对话框_布局);
//设置自定义对话框组件-文本、图像和按钮
imageView=(imageView)dialog.findViewById(R.id.biger\u image);
setImageBitmap(exListAdapter.getImageByPosition(lastClickedGroup,arg2));
pre=(ImageButton)dialog.findViewById(R.id.btn_pre);
后退=(ImageButton)dialog.findViewById(R.id.btn\u后退);
next=(ImageButton)dialog.findViewById(R.id.btn_next);
//如果单击按钮,则关闭自定义对话框
pre.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
dialog.dismise();
}
});
back.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
dialog.dismise();
}
});
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
dialog.dismise();
}
});
/*这是错误代码*/dialog.show()中的第269行;
}
}); 

那么我如何解决这个错误呢?

这样做

final Dialog dialog = new Dialog(MainActivity.this);

如果传递了错误的上下文,则会发生此错误,只需将
Dailog上下文更改为活动上下文

使用:


最终对话框=新对话框(MainActivity.this)

相反:


最终对话框=新对话框(getBaseContext())

更改最终版本

Dialog dialog = new Dialog(getBaseContext());


最终对话框=新对话框(MainActivity.this)


最终对话框=新建对话框(getApplicationContext())

对话框可以由活动而不是(直接)由应用程序(主要不是由服务)显示,因此对话框的构造函数需要的是活动的上下文,而不是应用程序的上下文

如果你知道你活动的名称,那么

final Dialog dialog = new Dialog(YOUR_ACTIVITY_NAME.this);
当然有用

但如果不想在代码中烧录活动名称,请使用以下代码段:

public class MainActivity extends Activity {
Context context; // for context of the application
Context acontext; // for context of the activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getApplicationContext(); // Don't use for Dialog
    acontext = this; // Use for Dialog
    setContentView(R.layout.activity_main);

    // Dialog block started
    final Dialog dialog = new Dialog(acontext);
    dialog.setContentView(R.layout.custom);
    dialog.setTitle("Title");
    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });     
    dialog.show();
    // Dialog block ended
}}

Direct hit------>>它正在工作。你能告诉我那里发生了什么吗。为什么getBaseContext()不工作?你不能使用baseContext(),因为它引用了其他上下文,UI需要当前上下文,所以你正在获取getBaseContext()Null有关更多信息,请访问此网站,感谢您的帮助。值得注意的是,在活动上下文中仍然会发生这种情况。您能否告诉我发生了什么。这与前面的答案一样有效。为什么getBaseContext()不起作用?您正在向对话框传递正确的上下文,这会导致错误,请参阅一些有关
上下文类型的文档
谢谢您的帮助。
public class MainActivity extends Activity {
Context context; // for context of the application
Context acontext; // for context of the activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getApplicationContext(); // Don't use for Dialog
    acontext = this; // Use for Dialog
    setContentView(R.layout.activity_main);

    // Dialog block started
    final Dialog dialog = new Dialog(acontext);
    dialog.setContentView(R.layout.custom);
    dialog.setTitle("Title");
    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });     
    dialog.show();
    // Dialog block ended
}}