在我的监听空指针异常 [英] Null Pointer Exception in my Listener

查看:228
本文介绍了在我的监听空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出我的AsyncTask的一个电话从我的片段课,当我尝试将数据发送回我的片段,我得到一个空指针异常。

I am making a call to my AsyncTask from my Fragment class, when i try to send the data back to my Fragment, i get a null pointer exception.

public interface AsyncListner { 
    public void onLoadComplete(List<DocumentResponse> documents);   
}

我的片段类实现的接口。

My Fragment class implements the interface.

public class FragmentClass extends SherlockFragment implements AsyncListner {@
    Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        new AsyncTask(Obj, getSherlockActivity()).setListener(this);
        new AsyncTask(Obj, getSherlockActivity()).execute();
    }

    @Override
    public void onLoadComplete(List < Response> data) {
        Log.d("Result", data.toString());
    }
}

我跟着这个答案。
http://stackoverflow.com/a/13476196/98514

推荐答案

您已经叫这创造两个不同的AsyncTasks两次:

You have called new twice which created two different AsyncTasks:

new DocumentsAsyncTask(documentsObject, getSherlockActivity()).setListener(this);
new DocumentsAsyncTask(documentsObject, getSherlockActivity()).execute();

您应该只创建一个

DocumentsAsyncTask task = new DocumentsAsyncTask(documentsObject, getSherlockActivity());
task.setListener(this);
task.execute();

注意区别?结果
在现有的code创建:

Notice the difference?
In your existing code you create:


  • 第一AsyncTask的,在这里设置监听器,但不叫的execute()

  • 第二AsyncTask的,其中有一个监听器,但你叫的execute()

  • the first AsyncTask, where you set the listener but don't call execute().
  • the second AsyncTask, which has a null listener but you do call execute().

在新的code,你有一个AsyncTask的其中同时做两。

In the new code, you have one AsyncTask which does both.

这篇关于在我的监听空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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