Hello friends, I have a class Asynctask
and I want to obtain the result of the method onPostExecute
that it generates when finishing in my Activity
main MainActivity
one, since the classes are separated, does anyone know how to do it? since I followed the steps of a possible solution in StackOverFlow in English :
But it gives me the following error:
> E/AndroidRuntime: FATAL EXCEPTION: main
> Process: com.appmovilperote, PID: 29566
> java.lang.NullPointerException: Attempt to invoke interface method 'void
> com.appmovilperote.AsyncTaskServicios.AsyncResponse.processFinish(java.lang.String)'
> on a null object reference
> at com.appmovilperote.AsyncTaskServicios.AsyncTaskDatos.onPostExecute(AsyncTaskDatos.java:48)
> at com.appmovilperote.AsyncTaskServicios.AsyncTaskDatos.onPostExecute(AsyncTaskDatos.java:25)
> at android.os.AsyncTask.finish(AsyncTask.java:636)
> at android.os.AsyncTask.access$500(AsyncTask.java:177)
> at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
> at android.os.Handler.dispatchMessage(Handler.java:102)
> at android.os.Looper.loop(Looper.java:155)
> at android.app.ActivityThread.main(ActivityThread.java:5725)
> at java.lang.reflect.Method.invoke(Native Method)
> at java.lang.reflect.Method.invoke(Method.java:372)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1030)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
Splash :
public class Splash extends Activity implements AsyncResponse {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
AsyncTaskDatos tel = new AsyncTaskDatos(Splash.this);
tel.execute();
}
@Override
public void processFinish(String output){
//Here you will receive the result fired from async class
//of onPostExecute(result) method.
}
}
asynctask :
public class AsyncTaskDatos extends AsyncTask<String, Void, String> {
private ProgressDialog dialog;
Splash activity;
public AsyncResponse delegate = null;
public AsyncTaskDatos(Splash activity) {
dialog = new ProgressDialog(activity);
this.activity = activity;
}
@Override
protected void onPreExecute() {
dialog.setMessage("Cargando datos por favor espere");
dialog.show();
}
@Override
protected void onPostExecute(String result) {
if (dialog.isShowing()) {
dialog.dismiss();
delegate.processFinish(result);
}
}
@Override
protected String doInBackground(String... params) {
String res = "1";
} catch (InterruptedException e) {
e.printStackTrace();
}
return res;
}
}
}
AsyncResponse :
public interface AsyncResponse {
void processFinish(String output);
}
How to get the return value of my AsyncTask's onPostExecute method in my Activity? Precisely with an interface like the one you declare:
Within your Asynctask you define a variable for the interface, which would be called at the end of the Asynctask execution
onPostExecute()
:You have this part correct, but the problem is mentioned in the error that indicates:
This means that the interface instance
AsyncResponse
is not initialized! Add a constructor that receives this instance:This way in the tu method
processFinish()
youActivity
would get the value obtained in theonPostExecute()
tu methodAsynctask
: