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

Android使用android.intent.action.SEND分享纯文本时遇实例化异常

解决Android分享纯文本时的ClassNotFoundException问题

看你遇到的情况:单张图片通过android.intent.action.SEND分享完全正常,但分享纯文本时就触发了类找不到的异常。结合报错信息,我帮你梳理几个最可能的原因和解决方案:

1. 先揪出最容易忽略的拼写错误

看报错里的类名:Revamp.RecieveDataFromApp.RecieveDataFromApp——这里的Recieve是拼写错误!正确的拼写应该是Receive(i和e的顺序搞反了)。

如果你的实际Activity类名是ReceiveDataFromApp,但清单文件或者代码里写成了Recieve,系统肯定找不到这个类。你需要:

  • 确认C#代码里的类定义(看你是Xamarin项目):
    // 检查命名空间和类名的拼写
    namespace Revamp.ReceiveDataFromApp {
        public class ReceiveDataFromApp : Activity {
            // ...
        }
    }
    
  • 同步修改AndroidManifest.xml里的组件声明,保证类名完全匹配:
    <activity android:name="Revamp.ReceiveDataFromApp.ReceiveDataFromApp">
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <!-- 同时支持文本和图片类型 -->
            <data android:mimeType="text/plain" />
            <data android:mimeType="image/*" />
        </intent-filter>
    </activity>
    

2. 确认Intent Filter的MIME类型配置

单图分享正常说明image/*的MIME类型配置没问题,但纯文本的text/plain可能没正确配置:

  • 如果你用Xamarin的特性配置(推荐,避免手动改清单出错),要给Activity添加两个IntentFilter分别处理文本和图片:
    [Activity(Label = "ReceiveDataFromApp")]
    // 处理纯文本分享
    [IntentFilter(new[] { Intent.ActionSend },
        Categories = new[] { Intent.CategoryDefault },
        DataMimeType = "text/plain")]
    // 处理图片分享
    [IntentFilter(new[] { Intent.ActionSend },
        Categories = new[] { Intent.CategoryDefault },
        DataMimeType = "image/*")]
    public class ReceiveDataFromApp : Activity {
        // ...
    }
    

3. 排查Xamarin的链接器优化问题

如果你的项目开启了链接器优化,它可能会把这个接收Activity当成“未使用代码”移除掉:

  • 在项目里新建一个LinkDescription.xml文件,内容如下:
    <?xml version="1.0" encoding="UTF-8" ?>
    <linker>
      <assembly fullname="Revamp">
        <type fullname="Revamp.ReceiveDataFromApp.ReceiveDataFromApp" preserve="all" />
      </assembly>
    </linker>
    
  • 记得把这个文件的Build Action设置为LinkDescription,然后重新编译部署。

4. 检查发送端的Intent构造

如果分享的发起代码是你自己写的,要确保纯文本分享的Intent设置了正确的MIME类型:

var sendIntent = new Intent(Intent.ActionSend);
sendIntent.SetType("text/plain"); // 这里必须是text/plain,不能错
sendIntent.PutExtra(Intent.ExtraText, "asdfdgdsfsa");
StartActivity(Intent.CreateChooser(sendIntent, "选择分享应用"));

5. 清理缓存重新部署

有时候是编译缓存搞的鬼:

  • 删除项目的bin和obj文件夹,重新生成解决方案。
  • 卸载设备上的旧应用,再重新部署,避免旧的编译残留导致冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:31:50