android - 即使 Activity 存在,ActivityNotFound

标签 android exception android-intent start-activity

<分区>

Possible Duplicate:
Intent for editing plain text file with the installed file editor (if any)

我正在尝试打开我下载的一些文本文件。当我有意开始 Activity 时,它会给出 ActivityNotFound 异常。

try {
    Uri path = Uri.parse(path+"/sampletext.txt");
    Intent intent = new Intent(Intent.ACTION_VIEW);
    PackageManager packageManager = ctx.getPackageManager();
    intent.setType("text/plain") ;
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    if (list.size() > 0 ) {
        intent.setDataAndType(path, "text/plain");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        ctx.startActivity(intent) ;
    } 

} catch (Exception e){
    e.printStackTrace() ;
}

我调试的时候发现list.size是1。

还有我遗漏的任何其他信息吗?

任何一种观点都会对我有所帮助。

谢谢。

最佳答案

ActivityNotFound 异常通常是在 list 文件中没有声明匹配的 Activity 或没有被另一个应用程序注册为共享 Activity 时遇到的。

您可能需要编辑您的路径:

Uri path = Uri.parse("file://"+ path + "/sampletext.txt");

更好的方法是使用便捷方法:

Uri data = Uri.fromFile(file);

但是您需要创建一个 File 对象才能使用该方法

关于android - 即使 Activity 存在,ActivityNotFound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12395290/

相关文章:

c++ - __try/__except block 或 try/catch block 哪个更好用?

android - 打开 Facebook 页面以分享图片

java - 从另一个包启动 Activity - 问题

android - 需要根据 Sensor.TYPE_ORIENTATION 数据计算旋转矢量

安卓 : Listen to packet data when Android in sleep mode

android - 如何使用导航组件将 fragment 实例保存在bottomNavigationView上

android - UIL图像显示在GridView中imageView的中心

当我们使用继承类时,c++ 捕获异常

c# - 如何避免 System.Runtime.InteropServices.COMException?

java - 使用 ObjectAnimator 对 SpannableStringBuilder 进行动画处理不会产生动画效果