當我在 Adnroid Simulator 上運行我的應用程式時,我沒有問題。當我嘗試在 Android 手機上運行該應用程式時,我收到錯誤訊息:
System.InvalidCastException
Unable to convert instance of type 'Android.Widget.ScrollView' to type 'AndroidX.AppCompat.Widget.Toolbar'.
除錯器停在這里:
base.OnCreate(savedInstanceState);
這是我在 Android 專案中的MainActivity.cs檔案:
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
namespace SmartCryptoWorld.Droid
{
[Activity(Label = "SmartCryptoWorld", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
我復制這部分代碼:
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
來自另一個專案,我不知道我是否真的需要它。我還使用這些檔案復制了資源下的檔案夾布局。
工具列.axml:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
Tabbar.axml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@ id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabIndicatorColor="@android:color/white"
app:tabGravity="fill"
app:tabMode="fixed" />
主.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
</LinearLayout>
我能做些什么來修復這個錯誤?
uj5u.com熱心網友回復:
由于您使用的是使用 AndroidX 的最新 Xamarin Forms Activity,因此您需要將舊庫轉換為 AndroidX 庫。
例如,將 XML 布局中的 <android.support.v7.widget.Toolbar/> 替換為 <androidx.appcompat.widget.Toolbar/>。
并將 android.support.design.widget.TabLayout 轉換為 com.google.android.material.tabs.TabLayout
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/396915.html
