I'm having some confusion on how to use Listeners to pass information between classes and activities.
The scenario is this:
I have a task that reads data from an Sqlite database and puts this data into an ArrayList that is passed to an Adapter which then displays it on the screen.
The question:
My app crashes when i attempt to update the listener interface with a value. It doesnt show anything in the logcat...it just hangs the app.
Could someone kindly point out where i'm going wrong. - Thanks
Details:
I have the database processing done by a class called DatbaseRoutines and within it an inner method doing the heavy lifting. the method is using AsyncTask so as not to hog the main thread.
I have created and Interface with a single method
public interface DatabaseProcessListener {
public void ProcessingIsDone(boolean blnProcessIsFinished);
}
In my DatabaseRoutines class i am creating a private variable
private DatabaseProcessListener dbProcesslistener;
along with a setter for it
public void setDbProcesslistener(DatabaseProcessListener dbProcesslistener) {
this.dbProcesslistener = dbProcesslistener;
}
When processing is done, i use the PostExecute method to update the Listener so that this can change the value in the listener to True (ie that db processing is done) below is the code
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
dialog.dismiss(); //i have progress dialog during processing and its
//closed here
if (blnUpdateSucceededStatus)
{
dbProcesslistener.ProcessingIsDone(true);
}
else
{
Toast.makeText(ctx, "Transactions Processing
failed",Toast.LENGTH_SHORT);
} ;
Finally in the activity displaying the data gathered from the database I Implement the listener. see below.
public class MyActivity extends Activity implements DatabaseProcessListener
{
@Override
public void ProcessingIsDone(boolean blnProcessIsFinished) {
if(blnProcessIsFinished){
PopulateExpandableListView();
}
public void PopulateExpandableListView() {
//code for populating the listview goes here;
}
}
after some digging i found the logcat error..its a nullpointer exception
05-05 11:11:18.590: ERROR/AndroidRuntime(22339): FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.mledger.DatabaseRoutines$1.onPostExecute(DatabaseRoutines.java:258)
at com.example.mledger.DatabaseRoutines$1.onPostExecute(DatabaseRoutines.java:141)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Unfortunately the app is hanging and the interface never updates the Activity that displays data with true (ie processing is done)
Do point me in the right direction. - Thanks
Sounds like the dbProcesslistener
variable is NULL
. Do you call your setter and pass it an initialized Listener object some time before you invoke dbProcesslistener.ProcessingIsDone(true);
?
Got the Answer! I was not initializing the listener in my calling class. all i needed to do was put in this line
db.setDbProcesslistener(this);
ie
DatabaseRoutines db = new DatabaseRoutines(MyActivity.this);
db.setDbProcesslistener(MyActivity.this);
and it started working.
Thanks to everyone who had a look and tried to think of the solution :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With