Android:单击Gallery元素时显示自定义Dialog

时间:2013-03-16 03:53:19

标签: android android-alertdialog android-gallery

Bonjour所有人

单击“图库”视图的元素时,我想显示带有自定义布局的对话框。我的图库显示没有任何问题,但在选择元素时发生错误。

我的代码:

gallery.setAdapter(new ImageAdapter(this));
...

gallery.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View v,  int position, long id) {


        final Dialog dialog = new Dialog(mContext);
        dialog.setContentView(R.layout.show_capture);

        dialog.setTitle("my title");

        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.ic_launcher);

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

        }
});

我的日志文件:

03-16 03:43:10.387: E/AndroidRuntime(5226): FATAL EXCEPTION: main
03-16 03:43:10.387: E/AndroidRuntime(5226): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.view.ViewRoot.setView(ViewRoot.java:531)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.app.Dialog.show(Dialog.java:241)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at com.xxx.activities.ActivityDetailsActivity$1.onItemClick(ActivityDetailsActivity.java:247)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.widget.Gallery.onSingleTapUp(Gallery.java:867)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.view.GestureDetector.onTouchEvent(GestureDetector.java:557)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.widget.Gallery.onTouchEvent(Gallery.java:842)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.view.View.dispatchTouchEvent(View.java:3885)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.os.Looper.loop(Looper.java:123)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at java.lang.reflect.Method.invokeNative(Native Method)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at java.lang.reflect.Method.invoke(Method.java:507)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-16 03:43:10.387: E/AndroidRuntime(5226):  at dalvik.system.NativeStart.main(Native Method)

有人知道如何解决它....

感谢您的阅读。

1 个答案:

答案 0 :(得分:1)

改变这个:

final Dialog dialog = new Dialog(mContext);

到此:

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