3

当我运行我的代码时,我收到此错误:

  : 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();
        }

    }); 

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

4

5 回答 5

9

这样做

final Dialog dialog = new Dialog(MainActivity.this);
于 2013-09-09T10:37:39.823 回答
2

改变最终

Dialog dialog = new Dialog(getBaseContext());

final Dialog dialog = new Dialog(YourActivity.this);
于 2013-09-09T10:52:10.667 回答
0

当传递错误的上下文时会发生此错误,只需更改Dailog context to activity context.

采用:

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

反而:

最终对话框 dialog = new Dialog(getBaseContext());

于 2013-09-09T10:38:29.437 回答
0

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

或者

最终对话框 dialog = new Dialog(getApplicationContext());

于 2013-09-09T11:14:58.937 回答
0

Dialog 可以由 Activity 显示,而不是(直接)由 Application(主要不是由 Service)显示,因此 Dialog 的构造函数需要 Activity 的 Context 而不是 Application 的 Context。

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

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

当然有效。

但是,如果您不想在代码中刻录 Activity 的名称,请使用以下代码段:

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
}}
于 2014-01-06T15:53:16.060 回答