NET MAUI集成Android原生XML布局触发ResourcesNotFoundException求助
在.NET MAUI中集成Android原生LinearLayout XML布局时遇ResourcesNotFoundException问题
我在.NET MAUI里尝试通过Handler集成Android原生LinearLayout的XML布局,步骤如下:
- 创建了
Platforms\Android\Resources\layout目录(MAUI默认无此目录),将XML布局文件放入其中,并设置Build Action为AndroidResource - 编写了对应的XAML页面与Handler代码
但点击按钮打开该ContentPage时,触发了Android.Content.Res.Resources+NotFoundException,错误栈显示找不到LinearLayout类,排查多时未解决,求解决思路。
布局XML代码
<?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"> <TextView android:id="@+id/my_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, World!" /> </LinearLayout>
XAML页面代码
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MauiApp102.Pages.page10" xmlns:androidWidget="clr-namespace:Android.Widget;assembly=Mono.Android;targetPlatform=Android" xmlns:android="http://schemas.android.com/apk/res/android" Title="page10"> <ContentView HeightRequest="400" WidthRequest="400"> </ContentView> </ContentPage>
Handler代码
Microsoft.Maui.Handlers.ContentViewHandler.Mapper.AppendToMapping("MyCustomization2", (handler, view) => #if ANDROID { handler.PlatformView.SetBackgroundResource(Resource.Layout.myxml); #endif });
错误信息
{Android.Content.Res.Resources+NotFoundException: Drawable com.companyname.mauiapp102:layout/layout2 with resource ID #0x7f0b0032 ---> Android.Content.Res.Resources+NotFoundException: File res/layout/layout2.xml from drawable resource ID #0x7f0b0032 ---> Android.Views.InflateException: Class not found LinearLayout ---> Java.Lang.ClassNotFoundException: Didn't find class "LinearLayout" on path: DexPathList[[zip file "/data/app/~.../com.companyname.mauiapp102-Sg4xnqr_Su3KqEezYw0ZLg==/base.apk"],nativeLibraryDirectories=[/data/app/~~fuxDokti2OmCHcE6YFiRuA==/com.companyname.mauiapp102-Sg4xnqr_Su3KqEezYw0ZLg==/lib/x86_64, /data/app/~~fuxDokti2OmCHcE6YFiRuA==/com.companyname.mauiapp102-Sg4xnqr_Su3KqEezYw0ZLg==/base.apk!/lib/x86_64, /system/lib64, /system_ext/lib64]]
解决思路与方案
1. 核心错误原因
你用错了API:SetBackgroundResource是用来设置Drawable资源的,但你传入的是Layout布局资源ID,Android会把Layout当成Drawable去解析,自然找不到LinearLayout类(Drawable体系中不存在这个类)。
2. 正确集成方式
要加载Android原生布局并作为ContentView的原生视图,需通过LayoutInflater去Inflate布局,再替换或添加到ContentView的原生视图容器中:
修改Handler代码如下:
Microsoft.Maui.Handlers.ContentViewHandler.Mapper.AppendToMapping("MyCustomization2", (handler, view) => #if ANDROID { // 获取Android系统的LayoutInflater实例 var inflater = Android.Views.LayoutInflater.From(handler.Context); // Inflate目标布局文件 var nativeLayout = inflater.Inflate(Resource.Layout.myxml, null); // 清空原PlatformView的子视图,添加新的原生布局 handler.PlatformView.RemoveAllViews(); handler.PlatformView.AddView(nativeLayout); #endif });
3. 额外排查点
- 确认XML布局文件名与代码中
Resource.Layout.myxml的名称完全一致(Android资源名区分大小写) - 右键布局文件→属性,确认Build Action确实设置为
AndroidResource - 清理并重建项目,避免资源缓存导致的异常:右键项目→清理,再右键项目→生成
内容的提问来源于stack exchange,提问作者en4orce
相关产品推荐
相关产品推荐

