2

MVVMCrossのProgressDialogに問題があります。setup.csからコンテキストがある場所を介しAndroid.Views.WindowManagerBadTokenExceptionて作成しているときに:を取得しています。ProgressDialogIReportService

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()。すべてのインターネットを検索しましたが、解決策が見つかりません。どんな提案も大歓迎です!

4

1 に答える 1

1

問題はAndroid1.6と同じだと思います:「android.view.WindowManager $ BadTokenException:ウィンドウを追加できません-トークンnullはアプリケーション用ではありません」またはダイアログを開こうとしたときにエラーが発生しました:android.view.WindowManager $ BadTokenException

つまり、次の行です。Context 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(); 
}
于 2012-08-07T14:46:58.123 に答える