我目前有一个带有一些
HTML电话号码和网址的TextView.
我注意到我们可以使用setMovementMethod使这些链接可以点击:
tv.setText("Phone number is: +32485123456 and url is http://www.blabla.com"); tv.setMovementMethod(LinkMovementMethod.getInstance());
这非常有效,但在某些设备上,它会因ActivityNotFoundException而崩溃,这是非常难以理解的. (见下面的代码)
这些问题出现在我的开发者控制台中,我无法重现它们.
我面临的一个大问题是我无法在我的活动中尝试/捕获代码,因为点击是由LinkMovementMethod处理的
关于如何避免此类错误的任何想法?
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.vlaamsbrabant.be/zoutleeuw (has extras) } at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378) at android.app.Activity.startActivityForResult(Activity.java:2817) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817) at android.app.Activity.startActivity(Activity.java:2923) at android.text.style.URLSpan.onClick(URLSpan.java:62) at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:216) at android.widget.TextView.onTouchEvent(TextView.java:6788) at android.view.View.dispatchTouchEvent(View.java:3766) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1896) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1159) at android.app.Activity.dispatchTouchEvent(Activity.java:2086) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1880) at android.view.ViewRoot.handleMessage(ViewRoot.java:1811) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4627) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) at dalvik.system.NativeStart.main(Native Method)
解决方法
您始终可以只扩展LinkMovementMethod并在那里捕获错误.
就像是:
private class MovementCheck extends LinkMovementMethod { @Override public boolean onTouchEvent( TextView widget,Spannable buffer,MotionEvent event ) { try { return super.onTouchEvent( widget,buffer,event ) ; } catch( Exception ex ) { Toast.makeText( MainActivity.this,"Could not load link",Toast.LENGTH_LONG ).show(); return true; } } }
另一种选择是使用this question中的方法提前确定是否准备好处理链接的任何内容.