【问题标题】:Android NPE while second calling interfaceAndroid NPE while 二次调用接口
【发布时间】:2016-07-05 11:40:28
【问题描述】:

我有一些课程 application class 扩展 Application。我将它用作填充数据库和执行 REST 调用的控制器。有一个接口:

private OnDBfilled onFilledListener;
...
public void setOnFilledListener(OnDBfilled onFilledListener) {
    this.onFilledListener = onFilledListener;
}
public interface OnDBfilled {
    void onFilledCallback();
}
...

当我得到所需的数据时,调用它来了解Fragment,该数据已成功从互联网加载并填充到数据库:

@Override
        public void onResponse(Call call, Response response) throws IOException {
...//working with data
onFilledListener.onFilledCallback();
}

第一次,当调用完成并填充数据库时,我可以在日志中看到结果:

private Application controller;
private Application.OnDBfilled listener;
...
controller = new Application();
listener = new Application.OnDBfilled() {
        @Override
        public void onFilledCallback() {
            System.out.println("bd is filled. replacing fragments initiated editianal");
            replaceFragment();
        }
    };
 controller.setOnFilledListener(listener);

这工作正常,我可以看到日志消息。但是当我尝试在另一个片段中做时,我在这一行中有Null Pointer Exception

 onFilledListener.onFilledCallback();

这是另一个片段的代码,但几乎相等:

  private Application controller;
private Application.OnDBfilled filledDBListener;

...
//onCreateView
controller = new Application();
    filledDBListener = new Application.OnDBfilled() {
        @Override
        public void onFilledCallback() {
            swipeRefreshLayout.setRefreshing(false);
           // fillEventListFromBD(UserData.getCity(getActivity()));
        }
    };
    controller.setOnFilledListener(filledDBListener);

有什么建议吗?谢谢!

【问题讨论】:

标签: android android-fragments interface


【解决方案1】:

OnDBfilled flid 静态

 private static OnDBfilled onFilledListener; 

片段和浏览器使用:

 viewpager.setoffscreenpagelimit(9)

【讨论】:

  • 你读过这个问题吗?从问题中引用:'我在这一行中有空指针异常:onFilledListener.onFilledCallback();'
猜你喜欢
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
  • 1970-01-01
相关资源
最近更新 更多