0

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

user1494865
  • 37
  • 1
  • 6

1 Answers1

2

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

        }
    });
Vishal Pawale
  • 3,416
  • 3
  • 28
  • 32
  • Since I do not have a Nexus 7 and can't reproduce the issue myself I have to wait some time until I see if it works or not. ... If there is another way I would prefer the other one since I don't know how much this method reduces the ui thread's speed. Well - lets see tomorrow – user1494865 May 13 '13 at 18:38
  • Okay..But do you understand the root cause? Because thats more important, you can accept the answer once you test it – Vishal Pawale May 13 '13 at 18:39
  • no, sorry. I don't know why inflating and edit this in ui thread should fix this problem :/ – user1494865 May 13 '13 at 18:47
  • Because all the UI Related operations should be done on UI thread only & `doInBackground` always excutes on background thread, thats why it was giving you the erroor `Can't create handler inside thread that has not called Looper.prepare()` whereas `onPostExecute` executes on UI thread – Vishal Pawale May 13 '13 at 18:51
  • App is now online for several hours and there are no more crashes. Your solution works for me. Just inflate the layout in UI thread and load the data in a background thread. Thank you! – user1494865 May 14 '13 at 05:59