如何管理[否应用程序可以执行此操作" (AKA ActivityNotFoundException) [英] How manage "No application can perform this action" (AKA ActivityNotFoundException)

查看:276
本文介绍了如何管理[否应用程序可以执行此操作" (AKA ActivityNotFoundException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我的code

    public void onClick() {
                try {
                     startActivity(Utils.openFile(f.getPath(),myExt));
                }
                catch(ActivityNotFoundException activityNotFoundException) {
                    Toast.makeText(mContext, "Nessuna App trovata", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse("market://search?q="+myExt+"&c=apps"));
                    startActivity(intent);
                }   

凡我Utils.openfile()是

Where my Utils.openfile() is

public static Intent openFile(String u,String b) {
    File file = new File(u);
    Intent target = new Intent(Intent.ACTION_VIEW);
    target.setDataAndType(Uri.fromFile(file),"application/" + b);
    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    Intent intent = Intent.createChooser(target, "Apri File");
    return intent;       
}

现在,你看,我想管理,如果不能发现,能打开我的文件(PDF,牧高笛或EPUB)任何应用程序,我想启动一个意图链接到Android市场和搜查任何应用程序..

Now as you see I'd like to manage that if can't found any app that can opens my file (pdf,mobi or epub) I'd like to start an intent to link to android market and search any apps..

不过,我不知道为什么,我的code从不运行到异常,但只是表明说不应用程序可以执行此操作对话框。我怎么能管理和实现我的目标?

But, I don't know why, my code never runs into exception but just show a dialog that say "No application can perform this action".. How could I manage and achieve my target?

推荐答案

尝试摆脱 createChooser的()。毕竟,这是不需要的 ACTION_VIEW 反正 - 如果用户选择了该MIME类型的默认设置,请让他们使用它。而且,选择器是preventing你获得 ActivityNotFoundException ,因为选择器的是活动,它总是存在的。

Try getting rid of createChooser(). After all, that's not needed for ACTION_VIEW anyway -- if the user chose a default for this MIME type, please allow them to use it. And, the chooser is preventing you from getting ActivityNotFoundException, because the chooser is the activity, and it always exists.

这篇关于如何管理[否应用程序可以执行此操作" (AKA ActivityNotFoundException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆