Android的定制对话框给出了一个错误 [英] Android custom dialog gives an error

查看:224
本文介绍了Android的定制对话框给出了一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的机器人应用我有一个按钮单击事件弹出一个对话框。但是,当我点击按钮的应用程序的停止工作。我加载pre布局设计到对话框。我将发布一个code段。

In my android app I have to popup a dialog on a button click event. But when I click on the button the app stops working. I load the pre designed layout into the dialog. I will post a code segment.

Button login = (Button) findViewById(R.id.btn_login);

    login.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            final Dialog dialog = new Dialog(getApplication());

            dialog.setContentView(R.layout.journey_details);
            dialog.setTitle("Android Custom Dialog Box");

            Button dialogButton = (Button) dialog.findViewById(R.id.btn_start_jrny);

            dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            dialog.show();

        }
    });

和它显示在登录猫以下错误

And It shows the following error in the Log cat.

     E/AndroidRuntime(1412): FATAL EXCEPTION: main
 E/AndroidRuntime(1412): Process: com.xont.geotracker, PID: 1412
 E/AndroidRuntime(1412): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
 E/AndroidRuntime(1412):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:538)
 E/AndroidRuntime(1412):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
 E/AndroidRuntime(1412):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
 E/AndroidRuntime(1412):    at android.app.Dialog.show(Dialog.java:286)
 E/AndroidRuntime(1412):    at com.xont.geotracker.Loging$1.onClick(Loging.java:61)
 E/AndroidRuntime(1412):    at android.view.View.performClick(View.java:4424)
 E/AndroidRuntime(1412):    at android.view.View$PerformClick.run(View.java:18383)
 E/AndroidRuntime(1412):    at android.os.Handler.handleCallback(Handler.java:733)
 E/AndroidRuntime(1412):    at android.os.Handler.dispatchMessage(Handler.java:95)
 E/AndroidRuntime(1412):    at android.os.Looper.loop(Looper.java:137)
 E/AndroidRuntime(1412):    at android.app.ActivityThread.main(ActivityThread.java:4998)
 E/AndroidRuntime(1412):    at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(1412):    at java.lang.reflect.Method.invoke(Method.java:515)
 E/AndroidRuntime(1412):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
 E/AndroidRuntime(1412):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
 E/AndroidRuntime(1412):    at dalvik.system.NativeStart.main(Native Method)

有人可以帮助我找出错误。需要帮忙。谢谢!

Can someone help me to figure out the error. Need help. Thank you!

推荐答案

改变

final Dialog dialog = new Dialog(getApplication());

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

如果你是里面的片段,更改为

and if you are inside Fragment, change to

final Dialog dialog = new Dialog(getActivity());

对话框要求上下文引用的窗口令牌不为空。这里的ApplicationContext窗口令牌是空在那里为活动将有它自己的窗口

Dialog requires a Context reference whose window token is not null. here ApplicationContext's window token is null where as Activity will have it's own window

这篇关于Android的定制对话框给出了一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆