showind无法运行ProgressDialog-BadTokenException [英] Unable run ProgressDialog - BadTokenException while showind

查看:86
本文介绍了showind无法运行ProgressDialog-BadTokenException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVVMCross中的ProgressDialog问题上遇到了问题. 我得到Android.Views.WindowManagerBadTokenException:通过IReportService创建ProgressDialog时,我从setup.cs获得上下文.

I am having a problem with a ProgressDialog thing in MVVMCross. I am getting Android.Views.WindowManagerBadTokenException: while creating ProgressDialog via IReportService where I have context from 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()时,调试器崩溃.我已经搜索了所有互联网,但找不到任何解决方案.任何建议都值得欢迎!

ToastMessages are working fine with using local context, but ProgressDialog doesn't. The debugger crashes while running _progressBar.Show(). I have searched all the internet but I can't find any solution. Any suggestions are more than welcome!

推荐答案

我怀疑问题与

即这行:上下文appContext = this.getApplicationContext(); 必须走,而是使用指向您所在活动的指针 (可能是这样).

i.e. this line: Context appContext = this.getApplicationContext(); must go, and instead you use a pointer to the activity you're in (probably this).

我今天也被这个咬了,烦人的部分是 getApplicationContext()是来自developer.android.com的逐字记录:(

I got bitten by this today too, the annoying part is the getApplicationContext() is verbatim from 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(); 
}

这篇关于showind无法运行ProgressDialog-BadTokenException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆