Reputation: 1185
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.
Upvotes: 2
Views: 4575
Reputation: 15199
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.
Upvotes: 4
Reputation: 34765
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();
}
});
}
Upvotes: 0
Reputation: 39397
Either dialog is null or callback is null.
Look at the line number, and you'll know which it is.
Upvotes: 0
Reputation: 13501
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
}
Upvotes: 1
Reputation: 42016
It may be possible that you dialog object gets null
so
instead
if(dialog.isShowing()){
dialog.dismiss();
try
if(dialog!=null){
dialog.dismiss();
Upvotes: 2