How to catch an exception (ActivityNotFoundException) when using LinkMovementMethod

I currently have a TextView with some phone numbers and an HTML url.

I noticed that we can use setMovementMethod to make these links available:

tv.setText("Phone number is: +32485123456 and url is http://www.blabla.com"); tv.setMovementMethod(LinkMovementMethod.getInstance()); 

This works very well, but on some devices it crashes with an ActivityNotFoundException, and this is pretty small. (see code below)

These problems appear in my developer console and I cannot reproduce them.

The big problem that I am facing is that I cannot try / catch the code in my activity, since the click is processed by LinkMovementMethod

Any idea on how I can avoid such errors?

 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) 
+6
source share
2 answers

You can always just extend LinkMovementMethod and catch an error there.

Sort of:

 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; } } } 

An error message appears if an application is not installed to process the intent.

Another option is to use the method in this question to determine in advance if something is prepared to process your links.

+18
source

If your textual representation of its origin is in xml, you can try aproach defined here .
Maby (I'm pretty sure about that). but the OS will handle this for u.

If this does not work, you should write device-independent code for tablets and phones. In this case, maby this one will send you. When his phone is enabled, you will be able to bind, and you won’t.

Maby shares 2 links in 2 text images, because os can in any case make one of the links (website), which, in its opinion, makes it clickable and crashes when you try to click on the number number

Hope this helps.

0
source

All Articles