处理程序返回空指针

时间:2014-12-12 00:22:09

标签: java android multithreading callback

我正在尝试运行以下代码:

public class MyListActivity extends ListActivity {

private static class MyListLoadingCallback implements Handler.Callback {
    private ListActivity activity;
    private MyListManager myListManager;

    public MyListLoadingCallback(ListActivity activity,
            MyListManager articleListManager) {
        this.activity = activity;
        this.myListManager = myListManager;
    }

    @Override
    public boolean handleMessage(Message message) {
        throw new NotYetImplementedException();
    }
}

private MyListManager myListManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myListManager.fetchArticles(new Handler(
            new MyListLoadingCallback(this, MyListManager)));

}

我在articleListManager.fetchArticles(new Handler(new MyListLoadingCallback(this,MyListManager)))中得到一个Null指针;

我的其他课程如下:

public class MyListManager  {

    public void fetchArticles(final Handler handler) {
    }
}

有人可以带我/帮助我使用这段代码以便更好地理解。我似乎并不完全理解它。

1 个答案:

答案 0 :(得分:1)

错误似乎很清楚:)。

只有一个"参考"在引发异常(myListManager.fetchArticles(new Handler(new MyListLoadingCallback(this, MyListManager)));)并且为myListManager的行中被访问,因此它必须是NULL。看看代码,你永远不会把它初始化为任何东西,所以它确实是NULL:)