You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI Blazor开发Android TV应用遇Activity实例化失败问题

问题分析

你遇到的ClassNotFoundException和应用无法启动的问题,根源有三点:

  1. AndroidManifest.xml存在重复的<application>标签,系统解析时会出现冲突,导致Activity配置失效。
  2. 手动指定的Activity类名com.companyname.testapp.MainActivity,和实际代码中的TestApp.MainActivity(命名空间为TestApp)不匹配,系统找不到对应类。
  3. MAUI的MainActivity已经通过MainLauncher = true标记了启动项,手动添加的intent-filter会和自动生成的配置冲突。
解决方案

1. 修复AndroidManifest.xml

删除重复的<application>标签,合并所有配置,只保留一个<application>节点,无需手动编写Activity节点(MAUI会自动生成)。正确的Manifest内容如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <uses-feature
        android:name="android.hardware.touchscreen"
        android:required="false" />

    <uses-feature android:name="android.software.leanback"
        android:required="true" />

    <application 
        android:allowBackup="true" 
        android:icon="@mipmap/ic_launcher" 
        android:roundIcon="@mipmap/appicon_round" 
        android:supportsRtl="true"
        android:label="MyApplication" 
        android:debuggable="true">
        <uses-library android:name="com.google.android.tv" android:required="false" />
    </application>
</manifest>

2. 修改MainActivity.cs,添加TV启动分类

在MainActivity的Activity特性下补充IntentFilter,添加Android TV所需的LEANBACK_LAUNCHER分类,让MAUI自动生成正确的Manifest配置:

using Android.App;
using Android.Content.PM;
using Android.OS;

namespace TestApp;

[Activity(
    Theme = "@style/Maui.SplashTheme", 
    MainLauncher = true, 
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density
)]
[IntentFilter(
    new[] { Android.Content.Intent.ActionMain },
    Categories = new[] { 
        Android.Content.Intent.CategoryLauncher, 
        Android.Content.Intent.CategoryLeanbackLauncher 
    }
)]
public class MainActivity : MauiAppCompatActivity
{
}

3. 清理并重建项目

  • 右键项目选择清理
  • 右键项目选择重建
  • 在模拟器中卸载现有应用,重新部署

完成以上步骤后,应用既能在Android TV导航菜单中显示,也能正常启动。

内容的提问来源于stack exchange,提问作者shalom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 22:45:45