我在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()
时调试器崩溃。我搜索了所有的互联网,但我找不到任何解决方案。任何建议都非常欢迎!
答案 0 :(得分:1)
我怀疑问题与Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"或Error trying to open a dialog: 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();
}