WindowManager$BadTokenException in Android

后端 未结 2 1304
攒了一身酷
攒了一身酷 2021-01-16 14:02

First of all, I\'m well aware that this error is occur because I\'m trying to call window/dialog through a Context that is not an Activity.

相关标签:
2条回答
  • 2021-01-16 14:31

    Pass Your Activity to showSplashScreen() Method...

    Do like this..

    HomeClass homeClass = new HomeClass();
    homeClass.showSplashScreen(Your Actvity);
    

    In Your Home class

    public void showSplashScreen(Activity curActivity) {        
     splashDialog = new Dialog(curActivity, R.style.SplashScreen);
     splashDialog.setContentView(R.layout.splash_screen);
     splashDialog.setCancelable(false);
     splashDialog.show();
    }
    
    0 讨论(0)
  • 2021-01-16 14:38

    I am going to modify your code, that maybe helpful for you...

    HomeClass homeClass = new HomeClass(this);
    homeClass.showSplashScreen();
    

    In Your Home class.. add parametric constructor..

    public class Home {
    private Context context;
    public Home(Context context){
    this.context = context;
    }
    public void showSplashScreen() {        
    splashDialog = new Dialog(context, R.style.SplashScreen);
     splashDialog.setContentView(R.layout.splash_screen);
     splashDialog.setCancelable(false);
    splashDialog.show();
    }
    
    0 讨论(0)
提交回复
热议问题