0

I'm using ThreadHandler in my app this way,

public class MessageThread extends HandlerThread {
    Handler mHandler;

    public MessageThread() {
        super("Message Thread");
    }

    public void queueProcessMessage(msgObject mObj) {

        mHandler.obtainMessage(PROCESS_MESSAGE, mObj).sendToTarget();

    }

    @Override
    protected void onLooperPrepared() {

       Commons.logIt("OnLoopPrep");

        mHandler = new Handler() {

            @Override
            public void handleMessage(Message msg) {

                switch (msg.what) {


                    case PROCESS_MESSAGE:
                        process_message((msgObject) msg.obj);
                        break;
                    default:

                        break;

                }
            }
        };
    }

   process_message(msgObject obj) {
        //Do stuff
   }

}

And I initialize it in the onCreate() like so,

public void onCreate(Bundle savedInstanceState) {

    MessageThread mThread = new MessageThread();
    mThread.start();
    mThread.getLooper();
    if (mThread != null)
        mThread.queueProcessMessage(Object);// this is the line the compiler is pointing at.
}

And it's working fine, the problem is that I needed to add something else where I have to start another activity waiting for result so I had to initialize/use it as shown below as the onActivityResult gets called before onCreate when I start an activity for result, this time i'm getting an error

java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Message android.os.Handler.obtainMessage(int, java.lang.Object)' on a null object reference

.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == IMAGE_ACTIVITY_REQUEST_CODE) {

        if (resultCode == Activity.RESULT_OK) {
           MessageThread mThread = new MessageThread();
           mThread.start();
           mThread.getLooper();

           mThread.queueProcessMessage(new msgObject("stuff"));
        }  

    }
  }

It looks like the thread is not getting initialize, what am I missing here?

Edit 1,

After discussing the issue here, it looks like it takes a bit of time for the handler to be initialized, I did this, it's ugly but it's working, any ideas of what should I do?

    public void queueProcessMessage(msgObject mObj) {
         while (mHandler == null)
            Log.i(TAG, "Not init yet"); //It keeps on looping here
        mHandler.obtainMessage(PROCESS_MESSAGE, mObj).sendToTarget();

    }
Adroid Freak
  • 329
  • 3
  • 18
  • What is `Object` and where is it defined/set? Also, please tell me you didn't name a variable reference after the `Object` class... – Ryan J Feb 25 '15 at 01:10
  • This is just an example of what I have, I'll update to avoid confusion. – Adroid Freak Feb 25 '15 at 01:15
  • "onActivityResult gets called before onCreate" that isn't actually possible. – Gabe Sechan Feb 25 '15 at 01:20
  • 1
    It is possible, https://groups.google.com/forum/#!topic/android-developers/3epIML7fjGw – Adroid Freak Feb 25 '15 at 01:24
  • onResume() is different from onCreate(). its impossible to call onActivityResult() before onCreate() – Randyka Yudhistira Feb 25 '15 at 01:28
  • Any reason not to initialize the handler inside queueProcessMessage if mHandler is null? – SilverCorvus Feb 25 '15 at 01:33
  • I have a log open on onCreate, I see it only the first time I open the activity, then when I start another activity from it and then go back, I don't the onCreate getting called. – Adroid Freak Feb 25 '15 at 01:36
  • @CurlyCorvus, I haven't thought about that actually. but the problem is that when I check mThread, it's not NULL. – Adroid Freak Feb 25 '15 at 01:39
  • @AdroidFreak if mHandler is not null when you enter queueProcessMessage, then this error will not happen. – SilverCorvus Feb 25 '15 at 01:49
  • @AdroidFreak Check your error closer... it's not being thrown where you think it is. `mHandler.obtainMessage(PROCESS_MESSAGE, mObj).sendToTarget();` is the line throwing the exception, because `mHandler` is `null`. – Ryan J Feb 25 '15 at 01:52
  • @RyanJ, yes exactly, I'm not sure what to do here? – Adroid Freak Feb 25 '15 at 02:00
  • @AdroidFreak where/when is this method called? `onLooperPrepared()` It's obviously not before you call `queueProcessMessage`, so you need to think about where you put your initializations... – Ryan J Feb 25 '15 at 02:03
  • @RyanJ, why it works on onCreate then? onLooperPrepared doc states this "Call back method that can be explicitly overridden if needed to execute some setup before Looper loops.", it's defently getting called before the queueProcessMessage. and if I initialize it inside queueProcessMessage then I'll end up with two handlers. – Adroid Freak Feb 25 '15 at 02:09
  • @RyanJ, I noticed that handler takes some time to get initialized, I updated the question with what I did. – Adroid Freak Feb 25 '15 at 02:37

0 Answers0