Android custom dialog gives an error

試著忘記壹切 提交于 2020-01-16 14:21:35

问题


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!


回答1:


change

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

to

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

and if you are inside Fragment, change to

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

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



来源:https://stackoverflow.com/questions/20779377/android-custom-dialog-gives-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!