从onPropertyChanged调用时对象引用更改(DataBinding Android)

时间:2017-04-25 08:06:33

标签: android dependency-injection mvp android-databinding

public class FilterFragmentExp extends BaseFragmentFilter implements ListingMVPview{
    @Inject PresenterListing presenterExp;
    private FilterUtilsModel filterModel;
    private ListingModel listingModel,listingModel_tmp;

    public static FilterFragmentExp newInstance(FilterUtilsModel filterModel,ListingModel listingModel) {
        FilterFragmentExp fragment = new FilterFragmentExp();
        Bundle bundle = new Bundle();
        bundle.putSerializable("filter",filterModel);
        bundle.putSerializable("listing",listingModel);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent().inject(this);
        presenterExp.attachView(this);
        receiveArguments();
        filterModel.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable observable, int i) {
                presenterExp.initialize(filterModel.getExpMap(),TYPE_EXP);
            }
        });
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        presenterExp.detachView();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_filter_exp;
    }

    @Override
    protected FilterUtilsModel getFilter() {
    }

    private void receiveArguments(){
        filterModel = (FilterUtilsModel) getArguments().getSerializable("filter");
        listingModel = (ListingModel) getArguments().getSerializable("listing");
        listingModel_tmp = listingModel;
    }



    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showRetry() {

    }

    @Override
    public void hideRetry() {

    }

    @Override
    public void showError(String message) {

    }
}

当我第一次移动到这个片段时,一切都运行良好,但从下一次(按下并再次返回)我从这一行得到一个nullpointer异常 -

presenterExp.initialize(filterModel.getExpMap(),TYPE_EXP);

我应该只在我没有将任何视图(MVP视图,这里是它的ListingMVPview)设置为presenter对象(presenterExp)时才会出现此错误,但我已将其设置在此行上

presenterExp.attachView(this);

这是我的演示者 -

    public class PresenterListing extends BasePresenter<ListingMVPview> {
        private final Context context;

        @Inject
        public PresenterListing(@ActivityContext Context context) {
            this.context = context;
        }

        @Override
        public void attachView(ListingMVPview mvpView) {
            super.attachView(mvpView);
        }

        @Override
        public void detachView() {
            super.detachView();
        }
        public void initialize(Map options, String type) {
            getMVPView().showLoading();
            this.getListing(options,type);
        }
....
    }

这里的上面代码getMVPView()返回null,虽然我已经设置了MVP视图。

我使用Dagger2进行依赖注入,并使用构造函数注入来创建 PresenterListing 对象并使用MVP架构。

任何线索都会有所帮助,因为当从onPropertyChanged调用此问题时,如果我将代码移动到其他地方(例如在某些视图上的onclicklistener),一切正常

1 个答案:

答案 0 :(得分:0)

你可以添加一个空判断 if (null) {...

建议开始使用google mvp,祝你好运