I got this new crash exception after the newest app update. It seems to not point anywhere. Would anyone be able to tell what the issue is? It looks like a possible email-forma
Copied from my other answer
I have already had the same issue, and I solved it by creating a new class that checks for null
before actually launching the Intent
.
All you have to do is to replace all URLSpan
spans, before setting the TextView
's text (which means you cannot use setAutoLinkMask()
).
This has to be done, because URLSpan
's onClick()
method does not perform any kind of null
checks.
How to preceed:
TextView txt = ...
txt.setLinksClickable(true);
txt.setText(SafeURLSpan.parseSafeHtml(<>));
txt.setMovementMethod(LinkMovementMethod.getInstance());
Kinds of strings that could be used in <
:
"Click here: My links"
"Mail me: My email"
... and so on...
Here is the source for SafeURLSPan
class (I use it in my app FPlay, and it has been tested on Android 10+):
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Browser;
import android.text.Html;
import android.text.Spannable;
import android.text.style.URLSpan;
import android.view.View;
public final class SafeURLSpan extends URLSpan {
public SafeURLSpan(String url) {
super(url);
}
@Override
public void onClick(View widget) {
try {
final Uri uri = Uri.parse(getURL());
final Context context = widget.getContext();
final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (context != null && intent != null) {
intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
context.startActivity(intent);
}
} catch (Throwable ex) {
}
}
public static CharSequence parseSafeHtml(CharSequence html) {
return replaceURLSpans(Html.fromHtml(html.toString()));
}
public static CharSequence replaceURLSpans(CharSequence text) {
if (text instanceof Spannable) {
final Spannable s = (Spannable)text;
final URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
if (spans != null && spans.length > 0) {
for (int i = spans.length - 1; i >= 0; i--) {
final URLSpan span = spans[i];
final int start = s.getSpanStart(span);
final int end = s.getSpanEnd(span);
final int flags = s.getSpanFlags(span);
s.removeSpan(span);
s.setSpan(new SafeURLSpan(span.getURL()), start, end, flags);
}
}
}
return text;
}
}