无法运行ProgressDialog - BadTokenException而showind(U

2019-09-20 23:30发布

我有在MVVMCross一个ProgressDialog的事情的问题。 我得到Android.Views.WindowManagerBadTokenException :创建ProgressDialog通过IReportService在那里我有从setup.cs上下文。

public class Setup
        : MvxBaseAndroidBindingSetup
    {
        public Setup(Context applicationContext)
            : base(applicationContext)
        {
        }

        protected override MvxApplication CreateApp()
        {
            return new NoSplashScreenApp();
        }
        public class Converters
        {
          public readonly MvxVisibilityConverter Visibility = new MvxVisibilityConverter();
        }
        protected override IEnumerable<Type> ValueConverterHolders
      {
          get { return new[] {typeof (Converters)}; }
      }
        protected override void InitializeLastChance()
        {
            var errorHandler = new ReportsDisplayer(ApplicationContext);
            base.InitializeLastChance();
        }

    }
public class ReportsDisplayer
: IMvxServiceConsumer<IReportsSource>
      , IMvxServiceConsumer<IMvxAndroidCurrentTopActivity>
{
    private readonly Context _applicationContext;
    private ProgressDialog _progressDialog;

    public ReportsDisplayer(Context applicationContext)
    {
        _applicationContext = applicationContext;

        var source = this.GetService<IReportsSource>();
        source.ErrorReported += (sender, args) => ShowError(args.Message);
        source.MessageReported += (sender, args) => ShowMessage(args.Title, args.Message);
        source.ProgressDialogShowed += (sender, args) => ShowProgressDialog(args.Title, args.Message);
        source.ProgressDialogDismiss += (sender, args) => DismissProgressDialog();

    }

    private void ShowError(string message)
    {
        var activity = this.GetService<IMvxAndroidCurrentTopActivity>().Activity as IMvxBindingActivity;
        View layoutView = activity.NonBindingInflate(Resource.Layout.ToastLayout_Error, null);

        var text1 = layoutView.FindViewById<TextView>(Resource.Id.ErrorText1);
        text1.Text = "Błąd";
        var text2 = layoutView.FindViewById<TextView>(Resource.Id.ErrorText2);
        text2.Text = message;

        var toast = new Toast(_applicationContext);

        toast.SetGravity(GravityFlags.CenterVertical, 0, 0);
        toast.Duration = ToastLength.Long;
        toast.View = layoutView;
        toast.Show();
    }
    private void ShowMessage(string title, string message)
    {
        var activity = this.GetService<IMvxAndroidCurrentTopActivity>().Activity as IMvxBindingActivity;
        View layoutView = activity.NonBindingInflate(Resource.Layout.ToastLayout_Message, null);
        var text1 = layoutView.FindViewById<TextView>(Resource.Id.MessageText1);
        text1.Text = title;
        var text2 = layoutView.FindViewById<TextView>(Resource.Id.MessageText2);
        text2.Text = message;

        var toast = new Toast(_applicationContext);
        toast.SetGravity(GravityFlags.CenterVertical, 0, 0);
        toast.Duration = ToastLength.Long;
        toast.View = layoutView;
        toast.Show();
    }
    private void ShowProgressDialog(string title, string message)
    {
        _progressDialog = new ProgressDialog(_applicationContext);
        _progressDialog .SetTitle(title);
        _progressDialog .SetMessage(message);
        _progressDialog .Show(); 
    }
    private void DismissProgressDialog()
    {
        _progressDialog .Dismiss();
    }
}

ToastMessages工作正常使用当地的情况,但ProgressDialog没有。 调试器崩溃时运行_progressBar.Show() 我已经找遍了所有的互联网,但我无法找到任何解决方案。 任何建议非常欢迎!

Answer 1:

我怀疑问题是一样的Android 1.6的:“android.view.WindowManager $ BadTokenException:无法添加窗口-令牌null不是一个应用程序”或错误试图打开一个对话框:android.view.WindowManager $ BadTokenException

即,该线:上下文appContext = this.getApplicationContext(); 一定要去,而是你使用指针来您所在的活动(可能这一点)。

我得到今天这个被咬过,恼人的部分是getApplicationContext()是逐字从developer.android.com :(

所以,请尝试:

private void ShowProgressDialog(string title, string message)
{
     var activity = this.GetService<IMvxAndroidCurrentTopActivity>().Activity;
    _progressDialog = new ProgressDialog(activity);
    _progressDialog .SetTitle(title);
    _progressDialog .SetMessage(message);
    _progressDialog .Show(); 
}


文章来源: Unable run ProgressDialog - BadTokenException while showind