Xamarin导致android 9崩溃

时间:2019-02-27 05:37:36

标签: android xamarin xamarin.forms android-9.0-pie

我开发了一个Xamarin Forms应用程序,该应用程序在Android 8上运行良好。但是,当我在Android 9上运行该应用程序时会崩溃。崩溃似乎发生在从一页切换到另一页或显示等待弹出窗口时。任何人都可以帮助确定仅在Android 9中发生的这些崩溃的原因。以下是debug的输出:

java.lang.IllegalArgumentException: Cannot set 'scaleX' to Float.NaN
android.view.View.sanitizeFloatPropertyValue()View.java:15549
android.view.View.sanitizeFloatPropertyValue()View.java:15523

android.view.View.setScaleX()View.java:14912
mono.android.animation.ValueAnimator_AnimatorUpdateListenerImplementor.n_onAnimationUpdate(Native Method)
mono.android.animation.ValueAnimator_AnimatorUpdateLis

tenerImplementor.onAnimationUpdate()ValueAnimator_AnimatorUpdateListenerImplementor.java:30
android.animation.ValueAnimator.animateValue()ValueAnimator.java:1547
android.animation.ValueAnimator.animateBasedOnTime()ValueAnimator.java:1339
android.animation.ValueAnimator.doAnimationFrame()ValueAnimator.java:1471
android.animation.AnimationHandler.doAnimationFrame()AnimationHandler.java:146
android.animation.AnimationHandler.access$100()AnimationHandler.java:37
android.animation.AnimationHandler$1.doFrame()AnimationHandler.java:54
android.view.Choreographer$CallbackRecord.run()Choreographer.java:1170
android.view.Choreographer.doCallbacks()Choreographer.java:984
android.view.Choreographer.doFrame()Choreographer.java:806
android.view.Choreographer$FrameDisplayEventReceiver.run()Choreographer.java:1158
android.os.Handler.handleCallback()Handler.java:873
android.os.Handler.dispatchMessage()Handler.java:99
android.os.Looper.loop()Looper.java:193
android.app.ActivityThread.main()ActivityThread.java:6863
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run()RuntimeInit.java:537
com.android.internal.os.ZygoteInit.main()ZygoteInit.java:858
Xamarin caused by: Java.Lang.IllegalArgumentException: Cannot set 'scaleX' to Float.NaN
Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualVoidMethod(JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args)<8acc8089c2ed40d08469fbaa6710a44c>:0
Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(string encodedMember, IJavaPeerable self, JniArgumentValue* parameters)<8acc8089c2ed40d08469fbaa6710a44c>:0
Android.Views.View.set_ScaleX(float value)Android.Views.View.cs:8908
Xamarin.Forms.Platform.Android.VisualElementTracker.UpdateScale()
Xamarin.Forms.Platform.Android.VisualElementTracker.HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
Xamarin.Forms.Platform.Android.VisualElementTracker.HandleRedrawNeeded(object sender, EventArg<T> e)
Xamarin.Forms.VisualElement.BatchCommit()
Xamarin.Forms.AnimationExtensions.HandleTweenerUpdated(object o, EventArgs args)
Xamarin.Forms.Tweener.<Start>b__22_0(long step)
Xamarin.Forms.Internals.Ticker.SendSignals(int timestep)
Xamarin.Forms.Platform.Android.AndroidTicker.OnValOnUpdate(object sender, ValueAnimator.AnimatorUpdateEventArgs e)
Android.Animation.ValueAnimator.IAnimatorUpdateListenerImplementor.OnAnimationUpdate(ValueAnimator animation)Android.Animation.ValueAnimator.cs:147
Android.Animation.ValueAnimator.IAnimatorUpdateListenerInvoker.n_OnAnimationUpdate_Landroid_animation_ValueAnimator_(IntPtr jnienv, IntPtr native__this, IntPtr native_animation)Android.Animation.ValueAnimator.cs:95
at (wrapper dynamic-method) System.Object.19(intptr,intptr,intptr)
--- End of managed Java.Lang.IllegalArgumentException stack trace ---
java.lang.IllegalArgumentException: Cannot set 'scaleX' to Float.NaN
android.view.View.sanitizeFloatPropertyValue()View.java:15549
android.view.View.sanitizeFloatPropertyValue()View.java:15523
android.view.View.setScaleX()View.java:14912
mono.android.animation.ValueAnimator_AnimatorUpdateListenerImplementor.n_onAnimationUpdate(Native Method)
mono.android.animation.ValueAnimator_AnimatorUpdateListenerImplementor.onAnimationUpdate()ValueAnimator_AnimatorUpdateListenerImplementor.java:30
android.animation.ValueAnimator.animateValue()ValueAnimator.java:1547
android.animation.ValueAnimator.animateBasedOnTime()ValueAnimator.java:1339
android.animation.ValueAnimator.doAnimationFrame()ValueAnimator.java:1471
android.animation.AnimationHandler.doAnimationFrame()AnimationHandler.java:146
android.animation.AnimationHandler.access$100()AnimationHandler.java:37
android.animation.AnimationHandler$1.doFrame()AnimationHandler.java:54
android.view.Choreographer$CallbackRecord.run()Choreographer.java:1170
android.view.Choreographer.doCallbacks()Choreographer.java:984
android.view.Choreographer.doFrame()Choreographer.java:806
android.view.Choreographer$FrameDisplayEventReceiver.run()Choreographer.java:1158
android.os.Handler.handleCallback()Handler.java:873
android.os.Handler.dispatchMessage()Handler.java:99
android.os.Looper.loop()Looper.java:193
android.app.ActivityThread.main()ActivityThread.java:6863
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run()RuntimeInit.java:537
com.android.internal.os.ZygoteInit.main()ZygoteInit.java:858

1 个答案:

答案 0 :(得分:0)

某些人可能会发现这很有用,在Xamarin Forms中使用ScaleTo()时出现此错误。该代码在 UWP 中工作正常,但是在Android上运行(我在Android KitKat和Android Pie上尝试过)时,遇到此错误:Java.Lang.IllegalArgumentException: 'Cannot set 'scaleX' to Float.NaN'

此错误是因为我在ScaleTo() async中使用了Task

这是我之前的代码

void StartTimer()
{
    Task.Run(async () =>
    {
       await TimerBar.ScaleTo(0, 5000, null);
    });
}

但是这给了我一个错误,因为我试图修改主线程以外的线程上的UI元素。请注意,所有UI活动均在主线程上进行。更改或修改UI元素或其属性不适用于除主线程之外的其他线程。在Xamarin Forms中,如果要将执行从主线程切换到另一个线程,请使用Device.BeginInvokeOnMainThread(async() => awaited code...;)。因此,我对代码进行了更改。

这是已修改的代码

void StartTimer()
{
    Device.BeginInvokeOnMainThread(async () =>
    {
        await TimerBar.ScaleTo(0, 5000, null));
    });
}

另一种方法是直接从XAML页面上单击按钮

<Button Text="Start Timer" Clicked="StartTimer" />
async void StartTimer(object sender, EventArgs e)
{
    await TimerBar.ScaleTo(0, 5000, null);
}