-1

In my application I want use dialog and for this I should set custom view to dialog.
I write below codes, but when running application show me ForceClose error on LogCat.

My Codes :

   if (!isPremium) {
        count = prefsUtils.getFromShared_INT(PrefsKeys.TRIAL_COUNT.name());
        prefsUtils.setToShared_INT(PrefsKeys.TRIAL_COUNT.name(), count + 1);
        if (count > 10) {
            final Dialog dialog = new Dialog(getApplicationContext());
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.dialog_end_trial);
            TextView dialogEndCount_premiumTxt = dialog.findViewById(R.id.dialogEndCount_premiumTxt);
            dialogEndCount_premiumTxt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, MainActivity2.class);
                    startActivity(intent);
                    finish();
                    dialog.dismiss();
                }
            });
            dialog.show();
        }
    }

Error message on LogCat :

 Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:702)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
    at android.app.Dialog.show(Dialog.java:337)
    at com.mcc.wpnews.activity.PostDetailsActivity.initView(PostDetailsActivity.java:176)
    at com.mcc.wpnews.activity.PostDetailsActivity.onCreate(PostDetailsActivity.java:104)
    at android.app.Activity.performCreate(Activity.java:6754)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2787) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6247) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 

How can I fix it?

RedBounce
  • 213
  • 5
  • 14

1 Answers1

0

Do not use getApplicationContext() unless you know why you are using getApplicationContext().

You cannot show a Dialog using the Application. So, replace new Dialog(getApplicationContext()) with new Dialog(this), and you should have better luck.

See this classic blog post from Dave Smith for more about when to use different types of Context.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491