显示对话框上的错误

时间:2013-09-25 13:04:47

标签: android

我厌倦了在自定义列表视图中显示自定义对话框。请求帮助

代码:

more.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog dialog = new Dialog(mContext.getApplicationContext());
                dialog.setTitle("Discription");
                dialog.setContentView(R.layout.dialog_item_simple_text);
                TextView textViewItem = (TextView) dialog
                        .findViewById(R.id.textViewItem);
                textViewItem.setText(data.short_description);
                dialog.setCancelable(true);
                dialog.setCanceledOnTouchOutside(true);
                dialog.show();//<--Error at this point
            }
        });

有错误: LogCat按摩

FATAL EXCEPTION: main android.view.WindowManager$BadTokenException: Unable to add
window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:563)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:269)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:281)
at com.fashionbargains.adapter.DnSAdapter$1.onClick(DnSAdapter.java:88)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

我有通过BaseTask类调用的自定义适配器类,该类是扩展AsyncTask

1 个答案:

答案 0 :(得分:0)

您需要参考当前活动,例如......

替换这个:

 Dialog dialog = new Dialog(mContext.getApplicationContext());

用这个:

Dialog dialog = new Dialog(YourClassname.this);

编辑:

如果您的onClick位于AsyncTask Class,则需要使用runOnUiThread

    runOnUiThread(new Runnable() {

                    @Override
                public void run() {
                    Dialog dialog = new Dialog(mContext.getApplicationContext());
                    dialog.setTitle("Discription");
                    dialog.setContentView(R.layout.dialog_item_simple_text);
                    TextView textViewItem = (TextView) dialog
                            .findViewById(R.id.textViewItem);
                    textViewItem.setText(data.short_description);
                    dialog.setCancelable(true);
                    dialog.setCanceledOnTouchOutside(true);
                    dialog.show();//<--Error at this point

    }
});