1

Possible Duplicate:
RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

I have a problem with using Java Serialization mechanism in Android. It works well when invoked from the UI thread, but when I try to use it from some background thread I get:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

Because of project nature I cannot deserialize everything in UI thread (also it should be possible to do it in background, so the UI will not stop responding).

BTW. Same thing happens when I try to deserialize something in background using SimpleXML.

So now we do deserialization (both XML and Java serialization) from UI thread which cannot be used everywhere.

Can anyone shed some light on this issue?

EDIT:

I'm using the following code to deserialize an object, it works well when called from UI thread.

public Object getObject(String key) throws InvalidClassException {
    Object result;
    try {
        FileInputStream fileIn = context.openFileInput(getPath(key));
        ObjectInputStream in = new ObjectInputStream(fileIn);
        result = in.readObject();
        in.close();
        fileIn.close();
    } catch (InvalidClassException e) {
        throw new InvalidClassException(e.getMessage());
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    return result;
}

EDIT 2

As mentioned in EJP comment below, I'm deserializing an Activity object. So I'm changing my questuion to: How to deserialize Activity object in a background thread? Not deserializing this object is an option that I'd rather avoid, because of performance issues (XML deserializes in about 4s while binary deserialization is less then 0.5s). I know that it would be possible to redesign our application, but due to project constraints, and it's extreme and unnecessary complexity, that's not really an option. Every bigger change is extremely painful.

So when issue is little clearer - does anyone have some ideas?

Thanks for any suggestions.

Community
  • 1
  • 1
kajman
  • 1,066
  • 11
  • 24
  • no one will going to spoon feed you and wouldn't provide you exact code. you have to apply little bit of your own brain on the basis of given suggestion. – dinesh sharma May 21 '12 at 10:16
  • @dinesh I am more than able to write my own code, just asking for some suggestions what can be wrong here. Why so harsh? – kajman May 21 '12 at 10:24
  • Why do you need a Looper in your background thread? – Lawrence D'Oliveiro May 21 '12 at 10:47
  • @LawrenceD'Oliveiro I don't need it (I think). Honestly I don't get loopers as I would like to. I don't think I'm creating any looper here though. – kajman May 21 '12 at 11:04

2 Answers2

0

Try and call Loooper.Prepare(); before your code and Looper.Loop(); after, workd for me.

Something like :

Looper.Prepare();
//your code
Looper.Loop();
eric.itzhak
  • 15,752
  • 26
  • 89
  • 142
  • I tried your suggestion, but problem is, that thread on which I call Looper.Prepare() and Looper.Loop() cannot get pass the line where I call Looper.Loop() (in debbuger it says "stepping" and it won't go pass the line) – kajman May 21 '12 at 10:26
  • But the object serialized correctly, and without any exceptions, so this may be good way to go. – kajman May 21 '12 at 10:29
0

you cannot do ui operations in any other thread all ui operations should be on mainthread

you can use this

   runOnUiThread(new Runnable() {

        @Override
        public void run() {
            code here

        }
    });