2

There are 2 Classes and 1 Interface: Class 1 = LoginPage, Class 2 = SyncData and Interface = AsyncTaskCompleteListener.

Class 1 uses Class 2 to sync information with the server. After Class 2 posts the result information in the onPostExecute method, the Interface callback method will be used to send the result information to Class 1. When I try to use the callback method, I get to see the following error:

04-12 11:06:43.715: W/dalvikvm(24941): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
04-12 11:06:43.720: E/AndroidRuntime(24941): FATAL EXCEPTION: main
04-12 11:06:43.720: E/AndroidRuntime(24941): java.lang.NullPointerException
04-12 11:06:43.720: E/AndroidRuntime(24941):    at com.on_d_mand.live_evenementen.SyncData.onPostExecute(SyncData.java:101)
04-12 11:06:43.720: E/AndroidRuntime(24941):    at com.on_d_mand.live_evenementen.SyncData.onPostExecute(SyncData.java:1)
04-12 11:06:43.720: E/AndroidRuntime(24941):    at android.os.AsyncTask.finish(AsyncTask.java:417)
04-12 11:06:43.720: E/AndroidRuntime(24941):    at android.os.AsyncTask.access$300(AsyncTask.java:127)
04-12 11:06:43.720: E/AndroidRuntime(24941):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
04-12 11:06:43.720: E/AndroidRuntime(24941):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 11:06:43.720: E/AndroidRuntime(24941):    at android.os.Looper.loop(Looper.java:130)
04-12 11:06:43.720: E/AndroidRuntime(24941):    at android.app.ActivityThread.main(ActivityThread.java:3691)
04-12 11:06:43.720: E/AndroidRuntime(24941):    at java.lang.reflect.Method.invokeNative(Native Method)
04-12 11:06:43.720: E/AndroidRuntime(24941):    at java.lang.reflect.Method.invoke(Method.java:507)
04-12 11:06:43.720: E/AndroidRuntime(24941):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
04-12 11:06:43.720: E/AndroidRuntime(24941):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
04-12 11:06:43.720: E/AndroidRuntime(24941):    at dalvik.system.NativeStart.main(Native Method)

Class 1 onTaskComplete method sample:

public void onTaskComplete(String result) {
    //perform action after receiving the information
    Toast.makeText(this, result, Toast.LENGTH_LONG).show(); 
}

Class 2 onPostExecute method sample:

public void onPostExecute(String RESULT) {

    if(dialog.isShowing()){
        dialog.dismiss();
    }

    callback.onTaskComplete(RESULT);

}

Class 3:

package com.on_d_mand.live_evenementen;

public interface AsyncTaskCompleteListener<T> {
    public void onTaskComplete(T result);
}

Does anyone knows what I'm doing wrong here? I hope it's not too complicated with the Classes and the Interface.

Edit

The solution for this problem was by initialising the callback object in the Class 2 contructor.

Aerial
  • 1,185
  • 4
  • 20
  • 42
  • Can you post the code where you initialise the 'callback' field of your AsyncTask? My suspicion is this either isn't being done, or is being done too late (e.g. after the task starts executing). – Jules Apr 12 '12 at 09:38
  • I can't initialise callback for some reason. When I try to initialise it, I get to see the following error message: Cannot instantiate the type AsyncTaskCompleteListener – Aerial Apr 12 '12 at 09:40
  • Can you post the code you're using when you try to initialise it? – Jules Apr 12 '12 at 09:48
  • I didn't initialise my Interface. What do I do? This is how it looks. AsyncTaskCompleteListener callback; – Aerial Apr 12 '12 at 09:51

5 Answers5

4

You need to pass a reference to an object that implements your listener to your 'SyncData' class when you create it. Presumably, at the moment in your LoginPage you have something that looks like this:

 new SyncData().execute();

You need to change this to:

 new SyncData(this).execute();

And add a constructor to SyncData:

 public SyncData(AsyncTaskCompleteListener<String> callback)
 {
      this.callback = callback;
 }

If you already have a constructor for SyncData, you will instead need to add the argument and code to the existing constructor.

Jules
  • 14,841
  • 9
  • 83
  • 130
  • Thank you very much! I didn't know how to initialise it... That was really stupid of me, but your code helped! – Aerial Apr 12 '12 at 09:57
  • I'm also new to Java and was wondering about how callback knows which onTaskComplete method it should trigger. There is other vice good answer about AsyncTasks and listeners but this piece of information was missing http://stackoverflow.com/questions/3291490/common-class-for-asynctask-in-android. And now when I'm looking that again. The information is in that answer also. Poor reading from me. – Mika Feb 21 '13 at 12:06
2

It may be possible that you dialog object gets null so instead

if(dialog.isShowing()){
        dialog.dismiss();

try

if(dialog!=null){
        dialog.dismiss();
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
1
    public void onPostExecute(String RESULT) {

        if(dialog.isShowing()){
            dialog.dismiss();
        }

        callback.onTaskComplete(RESULT); //make sure callback is not null

// and proper null check is required in onTaskComplete in case RESULT is null

    }
ngesh
  • 13,398
  • 4
  • 44
  • 60
  • I can't init callback. I tryed: callback = new AsyncTaskCompleteListener(); but I get to see: Cannot instantiate the type AsyncTaskCompleteListener – Aerial Apr 12 '12 at 09:37
  • @Xonarial .. is **AsyncTaskCompleteListener** a class or interface..? – ngesh Apr 12 '12 at 09:41
  • I did implement the interface in Class 1. What do I need to do now? – Aerial Apr 12 '12 at 09:43
  • if Class1 is an Activity you are not doing the right thing.. but you can implement in some other class and then create instance using **new** operator and call the method.. you can never use **new** with an interface since it does't have constructer.. – ngesh Apr 12 '12 at 09:45
  • So what is the best way to solve this issue? What do you suggest? – Aerial Apr 12 '12 at 09:48
  • @Xonarial I suggest you either implement this interface in the ACtivity which calls this asyncTAsk... its meaning full – ngesh Apr 12 '12 at 09:50
  • I already did that, the problem was initialising the callback. But still, thank you for your help. +1 – Aerial Apr 12 '12 at 10:04
0

Either dialog is null or callback is null.

Look at the line number, and you'll know which it is.

njzk2
  • 38,969
  • 7
  • 69
  • 107
0

try this code:::

public void onTaskComplete(String result) {
        runOnUiThread(new Runnable() {          
            public void run() {
                // TODO Auto-generated method stub
                //perform action after receiving the information
                Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show(); 
            }
        });
        }
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • That's not what I want, I need to use the callback interface to continue with what I want. :) – Aerial Apr 12 '12 at 09:45