Xamarin AndroidX 迁移:应用程序启动时崩溃(System.InvalidCastException:AndroidX.AppCompat.Widget.Toolbar) [英] Xamarin AndroidX Migration: Crash on App Startup (System.InvalidCastException: AndroidX.AppCompat.Widget.Toolbar)
问题描述
我最近将我的应用更新为 Xamarin Forms 5.0,因此更新为 AndroidX.我升级了所有包,这些包仍然引用了旧的 Android 支持库,并用来自 AndroidX 的等效引用替换了我的代码中所有出现的 Android 支持库引用.
I recently updated my App to Xamarin Forms 5.0 and therefore to AndroidX. I upgraded all packages, which still referenced the old Android Support Libraries and replaced all occurrences of Android Support Library References in my code with the equivalent reference from AndroidX.
但我的应用程序在启动时一直崩溃,以下异常:
But my App keeps crashing on startup, with following exception:
[AndroidRuntime] android.runtime.JavaProxyThrowable: System.InvalidCastException: Unable to convert instance of type 'Android.Widget.LinearLayout' to type 'AndroidX.AppCompat.Widget.Toolbar'.
[AndroidRuntime] at Java.Interop.JavaObjectExtensions.CastClass (Android.Runtime.IJavaObject instance, System.Type resultType) [0x0005d] in <1410c4afb1fd4823a12ecf897dd92287>:0
[AndroidRuntime] at Java.Interop.JavaObjectExtensions._JavaCast[TResult] (Android.Runtime.IJavaObject instance) [0x00052] in <1410c4afb1fd4823a12ecf897dd92287>:0
[AndroidRuntime] at Java.Interop.JavaObjectExtensions.JavaCast[TResult] (Android.Runtime.IJavaObject instance) [0x00000] in <1410c4afb1fd4823a12ecf897dd92287>:0
[AndroidRuntime] at Android.Runtime.Extensions.JavaCast[TResult] (Android.Runtime.IJavaObject instance) [0x00000] in <1410c4afb1fd4823a12ecf897dd92287>:0
[AndroidRuntime] at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.OnCreate (Android.OS.Bundle savedInstanceState, Xamarin.Forms.Platform.Android.ActivationFlags flags) [0x00090] in <04c545f414d24a37af95d995791bb9a9>:0
[AndroidRuntime] at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x00000] in <04c545f414d24a37af95d995791bb9a9>:0
[AndroidRuntime] at MyAppDemo.Droid.MainActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x00000] in <bdc0728e30d74980ad82869dc2b7bae4>:0
[AndroidRuntime] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <1410c4afb1fd4823a12ecf897dd92287>:0
[AndroidRuntime] at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.3(intptr,intptr,intptr)
[AndroidRuntime] at crc648c69828764ca3d7c.MainActivity.n_onCreate(Native Method)
[AndroidRuntime] at crc648c69828764ca3d7c.MainActivity.onCreate(MainActivity.java:43)
[AndroidRuntime] at android.app.Activity.performCreate(Activity.java:8000)
[AndroidRuntime] at android.app.Activity.performCreate(Activity.java:7984)
[AndroidRuntime] at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
[AndroidRuntime] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3404)
[AndroidRuntime] at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595)
[AndroidRuntime] at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
[AndroidRuntime] at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
[AndroidRuntime] at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
[AndroidRuntime] at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
[AndroidRuntime] at android.os.Handler.dispatchMessage(Handler.java:106)
[AndroidRuntime] at android.os.Looper.loop(Looper.java:223)
[AndroidRuntime] at android.app.ActivityThread.main(ActivityThread.java:7660)
[AndroidRuntime] at java.lang.reflect.Method.invoke(Native Method)
[AndroidRuntime] at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
[AndroidRuntime] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
这是我的MainActivity.OnCreate()
,异常信息中提到:
[Activity(ResizeableActivity = true, Label = "My App", Icon = "@drawable/ic_launcher", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, Theme = "@style/MyTheme", LaunchMode = LaunchMode.SingleTop)]
public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
ToolbarResource = Resource.Layout.toolbar;
TabLayoutResource = Resource.Layout.tabs;
...
}
}
这是我的工具栏(在 Resources>layout>toolbar.axml
下):
And this is my toolbar (Under Resources>layout>toolbar.axml
):
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar // <- before replacing: android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
我研究了 AndroidX 迁移教程,在不同的论坛上做了很多研究.我发现有类似问题的人,但对于他们从设备上卸载应用程序并删除/bin 和/obj 文件夹有帮助.对我来说,根本没有什么能解决它.
I studied the AndroidX Migration Tutorials, did a lot of research in different forums. I found people with similar issues, but for them uninstalling the app from the device and deleting /bin and /obj folders helped. For me nothing fixed it at all.
(顺便说一句:我的应用程序的 iOS 版本正在运行,当然)
(Btw: My iOS Version of the App is running, of course)
推荐答案
解决方案:删除工具栏.axml 将修复此错误.
Solution: Remove the toolbar.axml would fix this error.
这篇关于Xamarin AndroidX 迁移:应用程序启动时崩溃(System.InvalidCastException:AndroidX.AppCompat.Widget.Toolbar)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!