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

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)))处获得了一个空指针

我的其他课程如下所示:

public class MyListManager  {

    public void fetchArticles(final Handler handler) {
    }
}


有人可以步行给我/用这段代码帮助我,以便更好地理解。我似乎不太了解。

最佳答案

该错误似乎很明显:)。

抛出异常(myListManager.fetchArticles(new Handler(new MyListLoadingCallback(this, MyListManager)));)的行中只有一个“引用”被访问,并且它是myListManager,因此它必须是NULL。看一下代码,您永远不会将其初始化为任何东西,因此它的确是NULL :)

07-26 02:46