1

I want to send some message from asytask to me main activity. I want to do this with message object (handler). in my main activity I created this

    final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        msg.toString(); 
    }

}; 

the object that I pass to asytask

 new splash(first.this,mHandler).execute();

and the asytask that send message to activity from this method

protected void onPostExecute(String result) {
    Message msg =  new Message();
    Bundle bundle = new Bundle();
    bundle.putString("ActivityName",this.newActivity);
    msg.setData(bundle);
    mHandler.sendMessage(msg);
    Dialog.dismiss();

the logcat

    09-29 11:55:41.631: E/AndroidRuntime(473): FATAL EXCEPTION: main
09-29 11:55:41.631: E/AndroidRuntime(473): java.lang.NullPointerException
09-29 11:55:41.631: E/AndroidRuntime(473):  at tools.splash.onPostExecute(splash.java:109)
09-29 11:55:41.631: E/AndroidRuntime(473):  at tools.splash.onPostExecute(splash.java:1)
09-29 11:55:41.631: E/AndroidRuntime(473):  at android.os.AsyncTask.finish(AsyncTask.java:417)
09-29 11:55:41.631: E/AndroidRuntime(473):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
09-29 11:55:41.631: E/AndroidRuntime(473):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
09-29 11:55:41.631: E/AndroidRuntime(473):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 11:55:41.631: E/AndroidRuntime(473):  at android.os.Looper.loop(Looper.java:123)
09-29 11:55:41.631: E/AndroidRuntime(473):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-29 11:55:41.631: E/AndroidRuntime(473):  at java.lang.reflect.Method.invokeNative(Native Method)
09-29 11:55:41.631: E/AndroidRuntime(473):  at java.lang.reflect.Method.invoke(Method.java:507)
09-29 11:55:41.631: E/AndroidRuntime(473):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-29 11:55:41.631: E/AndroidRuntime(473):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-29 11:55:41.631: E/AndroidRuntime(473):  at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

2

According to your comments above the application crashes on Dialog.dismiss(). Dialog variable is correctly declared and instatiated? Are you making it null somewhere? (I suppose Dialog is a variable and not a class, right?)

EDIT:

OK, but the problem still remains the same. Have you checked that the mHandler object is properly declared instatiated and it is not null?

You are passing it as a param in the AsyncTask. Can you post the code where you take the mHandler and you store it "somewhere"?

Is the AsyncTask in the same scope of the Handler?

于 2012-09-29T12:35:26.507 に答える