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

Android跨应用传递目的地值失败:Intent调用报错求助

解决跨应用Intent传递数据的问题

我来帮你排查这个跨应用跳转的问题,咱们一步步拆解解决:

一、先搞定 ActivityNotFoundException(找不到Activity处理Intent)

你当前用的是隐式Intent,这种方式在跨应用场景下很容易因为匹配规则不严谨、系统限制(比如Android 11+的包可见性)失败,更可靠的方案是用显式Intent直接指定目标App的包名和Activity全类名:

修改App1的发送代码:

// 显式指定目标App的包名和Activity完整类名
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.myapp.driver", "com.myapp.driver.Activities.DriverTracking"));
intent.putExtra("destination", destination);
// 先判断是否有Activity能处理这个Intent,提前避免崩溃
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
} else {
    Log.e("APP1", "司机应用未安装或目标Activity不存在");
}

同步检查App2的Manifest配置:

跨应用调用的Activity必须设置android:exported="true"(Android 12+强制要求),同时确保Activity声明完整:

<activity
    android:name=".Activities.DriverTracking"
    android:exported="true"> <!-- 重点!跨应用调用必须开启这个属性 -->
    <!-- 如果还需要支持隐式Intent调用,保留下面的filter;只用显式Intent的话可以删掉 -->
    <intent-filter>
        <action android:name="com.myapp.driver.Activities.DriverTracking" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

二、解决 NullPointerException(Intent对象为空)

从错误日志看,你在sendIntent方法里调用intent.putExtras()时,intent变量是null。检查这几点:

  • 确保你正确初始化了Intent对象,比如有没有漏写new Intent()?
  • 检查变量名拼写,是不是把intent写成了其他未初始化的变量?

给你一个正确的初始化示例:

public void sendIntent(String destination) {
    // 先初始化Intent,避免null
    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.myapp.driver", "com.myapp.driver.Activities.DriverTracking"));
    intent.putExtra("destination", destination);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}

三、Android 11+ 额外配置(包可见性)

如果你的App1是Android 11(API 30)及以上版本,需要在App1的Manifest里添加对App2的包可见性声明,否则系统会限制App1访问App2:

<queries>
    <package android:name="com.myapp.driver" />
</queries>

四、App2接收数据的代码优化

接收数据时建议先判断Bundle是否为空,避免空指针:

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    mDestination = bundle.getString("destination");
    Log.d("STATUS_1", "Destination = " + mDestination);
} else {
    Log.d("STATUS_1", "未接收到任何数据");
}

按照上面的步骤调整后,应该就能顺利实现跨应用的数据传递了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:11:01