user1494865
user1494865

Reputation: 37

Android TextView append SpannableString forcing java.lang.RuntimeException on Nexus 7

I have a serious problem which crashes my app.

I'm loading some text in the app into a TextView. Some parts of this text are clickable and should call another activity - this works fine for most devices. Unfortunatelly it does not work on Nexus 7 devices and I really don't know why.

This is the stacktrace I get from Google:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at android.os.Handler.(Handler.java:197) at android.os.Handler.(Handler.java:111) at android.widget.Editor$UserDictionaryListener.(Editor.java:3836) at android.widget.Editor.(Editor.java:192) at android.widget.TextView.createEditorIfNeeded(TextView.java:8475) at android.widget.TextView.setText(TextView.java:3611) at android.widget.TextView.setText(TextView.java:3554) at android.widget.TextView.append(TextView.java:3277) at android.widget.TextView.append(TextView.java:3267) [...some app methods but not relevant to this problem...]

The java code:

final String text = displayedText;
SpannableString link = makeLinkSpan(displayedText, new View.OnClickListener() {          
    @Override
    public void onClick(View v) {
        //call the activiy
    }
});

textView.append(link);

The stacktrace tells me to call Looper.prepare() but I don't know where... I'm loading some data in an AsyncTask. A layout was inflated inside the doInBackground method and the textView is just a child of it. I don't see any reasson why this problem only affects my Nexus 7 users and not my devices. Even the emulator with Nexus 7 preset is working without any problem

Any Ideas how to solve this? :D

Upvotes: 0

Views: 732

Answers (1)

Vishal Pawale
Vishal Pawale

Reputation: 3476

You need to inflate layout on main thread. You can do this->

runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO Inflate layout & set textview's text here

        }
    });

Upvotes: 2

Related Questions