URLSpan
在6.0以下如果点击之后为找不到浏览器,就会报ActivityNotFoundException
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=http://www.baidu.com(has extras) }
at android.app.Instrumentation.null checkStartActivityResult(null)(Instrumentation.java:1781)
at android.app.Instrumentation.null execStartActivity(null)(Instrumentation.java:1501)
at android.app.Activity.null startActivityForResult(null)(Activity.java:3788)
at android.support.v4.app.BaseFragmentActivityApi16.void startActivityForResult(android.content.Intent,int,android.os.Bundle)(BaseFragmentActivityApi16.java:54)
-
源码
//源码5.1.0 @Override public void onClick(View widget) { Uri uri = Uri.parse(getURL()); Context context = widget.getContext(); Intent intent = new Intent(Intent.ACTION_VIEW, uri); intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()); context.startActivity(intent); } //6.1.0 @Override public void onClick(View widget) { Uri uri = Uri.parse(getURL()); Context context = widget.getContext(); Intent intent = new Intent(Intent.ACTION_VIEW, uri); intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()); try { context.startActivity(intent); } catch (ActivityNotFoundException e) { Log.w("URLSpan", "Actvity was not found for intent, " + intent.toString()); } }
-
解决办法
URLSpan urlSpan; if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) { urlSpan = new URLSpan(uri); }else { urlSpan=new URLSpan(uri){ @Override public void onClick(View widget) { try { super.onClick(widget); }catch (ActivityNotFoundException e){ e.printStackTrace(); } } }; }