触发事件时的AsyncTask完成使用监听器 [英] Trigger event when AsyncTask is complete using Listener

查看:269
本文介绍了触发事件时的AsyncTask完成使用监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于如何使用监听器通过课程和活动之间的信息有些混乱。

I'm having some confusion on how to use Listeners to pass information between classes and activities.

该方案是这样的:
我有从SQLite数据库读取数据并把这种数据转换成被传递给一个适配器,然后在屏幕上显示它的ArrayList一个任务。

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.

问题:
当我试图更新一个值,侦听器接口我的应用程序崩溃。它不显示在logcat中什么...它只是挂起的应用程序。 可能有人好心指出我要去哪里错了。 - 谢谢

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

详细信息:
我有一类叫做DatbaseRoutines并在其内部方法做繁重所做的数据库处理。该方法是使用的AsyncTask以免养猪主线程。

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);
} 

在我DatabaseRoutines类我创建一个私有变量

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;
    }

在加工完成后,我用的是PostExecute方法来更新侦听器,这样可以在收听值更改为真(即数据库处理完成) 下面是code

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;
     }
}

经过一番挖掘,我发现了logcat的error..its一个空指针异常

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

推荐答案

得到了答案!我没有初始化监听我的电话类。所有我需要做的是把这个行

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);

DatabaseRoutines db = new DatabaseRoutines(MyActivity.this);
db.setDbProcesslistener(MyActivity.this);

和它开始工作。

谢谢大家谁一看,并试图想解决的:)

Thanks to everyone who had a look and tried to think of the solution :)

这篇关于触发事件时的AsyncTask完成使用监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆