1

I have a TextView with SpannableString because I want the do differnet thing when user click different position of the view. Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TextView tv = new TextView(this);
    String str = "ClickMe";
    SpannableString spStr = new SpannableString(str);
    ClickableSpan clickSpan = new CustomizedClickableSpan(str);
    spStr.setSpan(clickSpan, 0, str.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    tv.setText("PlainTextA");
    tv.append(spStr);
    tv.append("PlainTextB");
    tv.setMovementMethod(LinkMovementMethod.getInstance());

    setContentView(tv);
}

private class CustomizedClickableSpan extends ClickableSpan {
    String text;

    public CustomizedClickableSpan(String text) {
        super();
        this.text = text;
    }

    @Override
    public void onClick(View widget) {
        Toast.makeText(SpanTextView.this, text, Toast.LENGTH_SHORT).show();
    }

}

After that, the textView's text is "PlainTextAClickMePlainTextB". And when I click the "ClickMe", the toast is showing. (It's just very well.)

But, when I long press the "ClickMe", the app crashed! Here is my log:

java.lang.NullPointerException
at android.widget.Editor.touchPositionIsInSelection(Editor.java:750)
at android.widget.Editor.performLongClick(Editor.java:851)
at android.widget.TextView.performLongClick(TextView.java:8390)
at android.view.View$CheckForLongPress.run(View.java:18419)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5050)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)

Anyone can help me? Thanks a lot!

EDIT:
Thanks for @Blackbelt . I just add these code:

tv.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        //just consume the event
        return true;
    }
});

And the problem is solved!
BTW, what's the reason of the NullPointerException?

Vladimir Markeev
  • 654
  • 10
  • 25
hanswim
  • 1,182
  • 8
  • 20

2 Answers2

2

You must add a method to catch the longClick event. In this case, if you want to control the TextView :

tv.setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {

        Toast.makeText(HomeSafeActivity.this, "Long preess", Toast.LENGTH_LONG).show();

        return true;
    }
});
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • This doesn't work. And, the super class ClickableSpan doesn't has method onLongClick. – hanswim May 05 '15 at 09:36
  • i know, that's why it was not annotated with `@Override`, check my update – Jordi Castilla May 05 '15 at 09:51
  • 2
    It works! Thank you very much. Actually I don't need the long click event, I just want to avoid the crash. – hanswim May 05 '15 at 10:06
  • @hanswim I've seen in your profile you havn't accepted any answer yet. If this or any answer has solved your question please consider upvoting or [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and **gives some reputation to both the answerer and yourself**. Of course there is no obligation to do this. – Jordi Castilla May 05 '15 at 10:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76981/discussion-between-hanswim-and-jordi-castilla). – hanswim May 05 '15 at 10:16
  • I got the same issue.I could fix this by setting font of the view(Editext/Textview) programmatically instead of XML.Please note that for me it was only happening on lollipop versions of Samsung reported by crashlytics. – Raj Trivedi Apr 19 '17 at 11:17
2

Solution with setOnLongClickListener has one side effect: LinkMovementMethod.onTouchEvent would not get MotionEvent.ACTION_UP. For avoiding this problem you can call TextView.setLongClickable(false) instead. It is important to call setLongClickable after setMovementMethod, because setMovementMethod calls setLongClickable(true) inside.

Irshad
  • 3,071
  • 5
  • 30
  • 51
  • This should be a comment on the answer you are referring to. – Blackwood Jan 25 '16 at 19:36
  • I got the same issue.I could fix this by setting font of the view(Editext/Textview) programmatically instead of XML.Please note that for me it was only happening on lollipop versions of Samsung reported by crashlytics. – Raj Trivedi Apr 19 '17 at 11:17