我正在使用Xamarin开发一个android应用程序。
我正在创建类似于android Toast.MakeText()
的自定义吐司消息。检查下面的我的C#.NET代码:
var @params = new WindowManagerLayoutParams(
ViewGroup.LayoutParams.WrapContent,
ViewGroup.LayoutParams.WrapContent,
WindowManagerTypes.Toast,
WindowManagerFlags.NotFocusable | WindowManagerFlags.NotTouchable,
Android.Graphics.Format.Translucent);
Android.Content.Res.Configuration config = activity.Resources.Configuration;
@params.Gravity = Gravity.GetAbsoluteGravity((GravityFlags.CenterHorizontal | GravityFlags.Bottom), (GravityFlags)config.LayoutDirection);
@params.X = 0;
@params.Y = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 64f, activity.Resources.DisplayMetrics);
@params.WindowAnimations = Android.Resource.Style.AnimationToast;
@params.PackageName = activity.PackageName;
@params.Token = activity.Window.Attributes.Token;
if ((@params.Gravity & GravityFlags.HorizontalGravityMask) == GravityFlags.FillHorizontal)
{
@params.HorizontalWeight = 1.0f;
}
if ((@params.Gravity & GravityFlags.VerticalGravityMask) == GravityFlags.FillVertical)
{
@params.VerticalWeight = 1.0f;
}
try
{
activity.WindowManager.AddView(msg.MView, @params);
}
catch(System.Exception ex)
{
}
将视图添加到Windowmanager
时,出现以下异常:
{Android.Views.WindowManagerBadTokenException:无法添加窗口-令牌android.os.BinderProxy@901e8d6无效;您的活动正在进行吗? 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在[0x0000c]中: 在<3cb12bce834a498199b2be6cd3e00922>:0中的Java.Interop.JniEnvironment + InstanceMethods.CallVoidMethod(Java.Interop.JniObjectReference实例,Java.Interop.JniMethodInfo方法,Java.Interop.JniArgumentValue * args)中[0x00069] 在Android.Runtime.JNIEnv.CallVoidMethod(System.IntPtr jobject,System.IntPtr jmethod,Android.Runtime.JValue * parms)中的[0x0000e],在:0中 在Android.Views.IWindowManagerInvoker.AddView中(Android.Views.View视图,Android.Views.ViewGroup + LayoutParams参数)在[0x0007c]中:: 0 在ToastCompat.cs:152中的GSP.Phone.Controls.ToastCompat + <> c__DisplayClass11_0.b__0()[0x00162]处 ---托管的Android.Views.WindowManagerBadTokenException堆栈结束跟踪- android.view.WindowManager $ BadTokenException:无法添加窗口-令牌android.os.BinderProxy@901e8d6无效;您的活动正在进行吗? 在android.view.ViewRootImpl.setView(ViewRootImpl.java:920) 在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:377) 在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:105) 在mono.java.lang.RunnableImplementor.n_run(本地方法) 在mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30) 在android.os.Handler.handleCallback(Handler.java:751) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6823) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1557) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445) }
在我看来,将令牌分配给窗口管理器参数时出现了问题。我是否将正确的令牌值(activity.Window.Attributes.Token)分配给窗口管理器参数?
答案 0 :(得分:0)
我已经安排好条件
override func viewDidLoad() {
super.viewDidLoad()
//based on the game title decide which game played
if passedTitle == "Trivia" {
questionAll = Trivia()
}else if passedTitle == "Diy" {
questionAll = Diy()
}
func process(answers: TypeOfAnswers) {
if let points = questionAllInstance.answers["level\(level)"] {
for singlePoint in points { // I replaced the forced unwrap to the optional binding above
let sumGrade = singlePoint.reduce(0, {$0 + $1})
totalGrade += sumGrade
}
}
}
switch questionAll {
case let trivia as Trivia: process(answers: trivia.answers)
case let diy as Diy: process(answers: diy.answers)
default: break
}
崩溃问题现在不再出现