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 支持库,并将我代码中所有出现的 Android 支持库引用替换为来自 AndroidX.
的等效引用
但是我的应用程序在启动时一直崩溃,但出现以下异常:
[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
下):
<?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 文件夹会有所帮助。对我来说,根本没有解决它。
(顺便说一句:我的 iOS 应用程序版本当然是 运行)
我在使用 Xamarin 时遇到了同样的问题,对我来说它有助于更改
下的链接属性
--> Project.android -> 属性 -> Anroid 选项 -> 链接
切换到 none,尝试启动您的项目,然后切换回 sdk 和用户程序集
这个是为我做的
解决方案:删除 toolbar.axml 将修复此错误。
我最近将我的应用更新为 Xamarin Forms 5.0,因此更新为 AndroidX。我升级了所有包,它们仍然引用旧的 Android 支持库,并将我代码中所有出现的 Android 支持库引用替换为来自 AndroidX.
的等效引用但是我的应用程序在启动时一直崩溃,但出现以下异常:
[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
下):
<?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 文件夹会有所帮助。对我来说,根本没有解决它。
(顺便说一句:我的 iOS 应用程序版本当然是 运行)
我在使用 Xamarin 时遇到了同样的问题,对我来说它有助于更改
下的链接属性--> Project.android -> 属性 -> Anroid 选项 -> 链接
切换到 none,尝试启动您的项目,然后切换回 sdk 和用户程序集
这个是为我做的
解决方案:删除 toolbar.axml 将修复此错误。