我在我的应用程序中使用AppIntro库.
它有3张幻灯片.我想在显示第三张幻灯片时询问用户.为实现这一点,我正在使用afollestad的材料对话框.
AppIntro活动中的代码如下所示:
@Override public void onNextPressed() { if(this.pager.getCurrentItem() == 2) { MaterialDialog dialog = new MaterialDialog.Builder(getApplicationContext()) .title("QR Code scannen") .content("Möchtest du den QR Code scannen oder selbst eingeben?") .positiveText("eingeben") .negativeText("scannen") .onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { Intent intent = new Intent(getApplicationContext(), RegistrationActivity.class); startActivity(intent); } }) .onNegative(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { // TODO } }) .show(); } }
运行应用程序当我滑动到第三张幻灯片时,我遇到以下问题:
com.afollestad.materialdialogs.MaterialDialog$DialogException: Bad window token, you cannot show a dialog before an Activity is created or after it's hidden. at com.afollestad.materialdialogs.MaterialDialog.show(MaterialDialog.java:1328) at com.afollestad.materialdialogs.MaterialDialog$Builder.show(MaterialDialog.java:1317) at com.ads.adstimer.fragment.Registration.RegistrationActivity.onNextPressed(RegistrationActivity.java:64) at com.github.paolorotolo.appintro.AppIntro$2.onClick(AppIntro.java:118) at android.view.View.performClick(View.java:5204) at android.view.View$PerformClick.run(View.java:21153) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我真的不明白这个问题.因为AppIntro活动已经加载了,我希望它在显示特定片段时显示对话框.
如何在显示特定幻灯片时显示对话框?
MaterialDialog dialog = new MaterialDialog.Builder(getApplicationContext())
我想你需要在Activity
这里传递实例,而不是应用程序上下文.
这可以通过确认当前来解决,Activity
hasWindowFocus
因为当您获得片段中的上下文时,它可能没有windowFocus
:
if (((Activity) mContext).hasWindowFocus()) { mDialog.show(); }
使用
MaterialDialog dialog = new MaterialDialog.Builder(YourActivityName.class)
不要使用getApplicationContext().