接口观察者模式:空对象引用 [英] Interface observer pattern: null object reference

查看:94
本文介绍了接口观察者模式:空对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定我的观察者模式有什么问题:

Not sure what's wrong with my observer pattern:

我已将界面定义为:

public interface OnBackFilterPressListener {
    public ArrayList<FoodType> filterFoodType ();
}

当我按下后退按钮时,我希望激活聆听者 - 所有此代码位于我的活动类中:

When I press the back button, I want the listener to be activated - all this code is in my activity class:

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    onBackFilterPressListener.filterFoodType();
}
}

在我的适配器类中,我有以下代码:

In my adapter class, I have the following code:

public class RandomRecyclerViewAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements SearchActivity.OnBackFilterPressListener {

       @Override
    public ArrayList<FoodType> filterFoodType() {
        return foodTypes;
    }

}

当我按下我的后退按钮时活动,我只想在我的活动类中返回我的适配器中的数据数组。

When I press the back button in my activity, I just want the array of data in my adapter to be returned in my activity class.

但是我收到此错误:


java.lang.NullPointerException:尝试调用接口方法
'java.util.ArrayList
com.example.simon..SearchActivity $ OnBackFilterPressListener.filterFoodType()'
对空对象引用

java.lang.NullPointerException: Attempt to invoke interface method 'java.util.ArrayList com.example.simon..SearchActivity$OnBackFilterPressListener.filterFoodType()' on a null object reference

foodType肯定存在 - 适配器将数据加载到活动中,我可以看到它在android模拟器上显示。当我点击后退按钮时,我收到此错误。

foodType definitely exists - the adapter loads the data into activity and I can see it being displayed on the android emulator. I get this error when I click the back button.

推荐答案

我通过关注此主题并有点创意来实现它:

I got it to work by following this thread and being a little creative:

如何在android中创建我们自己的Listener接口?

我写了一个自定义对象来防止空对象引用使用接口,我会首先要创建对象:

I wrote a custom object to prevent the null object reference as to use the interface, I would first have to create the object:

public class BackFilterEvent implements Serializable {

    private OnEventListener mOnEventListener;

    public void setOnEventListener(OnEventListener listener) {
        mOnEventListener = listener;
    }

    public ArrayList<FoodType> doEvent() {
        if (mOnEventListener != null) {
            return mOnEventListener.onEvent(); 
        }
        return null;
    }

    public interface OnEventListener {
        ArrayList<FoodType> onEvent();
    }

}

然后在我的Activity类中,我创建了对象:

Then in my Activity class, I created the object:

BackFilterEvent backFilterEvent = new BackFilterEvent();

我尽可能地将其传递给我的代码。在我的例子中,我有一个活动,在片段中使用recyclerview适配器提供片段。我传递了这个对象,直到它到达recyclerview适配器。

And I pass it as far as it needs to go in my code. In my case, I had an activity that feed a fragment with an recyclerview adapter within the fragment. I passed this object until it got to the recyclerview adapter.

将对象从Activity传递给Fragment:

Passing object from Activity to Fragment:

            FilterSearchFragment filterSearchFragment = new FilterSearchFragment();
            Bundle bundle = new Bundle();
            bundle.putSerializable("listener", backFilterEvent);
            filterSearchFragment.setArguments(bundle);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, filterSearchFragment)
                    .commit();

从片段传递到适配器:

    Bundle bundle = getArguments();
    BackFilterEvent backFilterEvent = (BackFilterEvent) bundle.getSerializable("listener");
    RandomRecyclerViewAdapter randomRecyclerViewAdapter  = new RandomRecyclerViewAdapter(getContext(), backFilterEvent);

在我的适配器构造函数中,我将它设置为从活动类调用它后执行的工作:

In my adapter constructor, I set it to do the work once I call it from the activity class:

    this.backFilterEvent = backFilterEvent;
    backFilterEvent.setOnEventListener(new BackFilterEvent.OnEventListener() {
        @Override
        public ArrayList<FoodType> onEvent() {
            return foodTypes;
        }
    });

在我的活动类的后退按钮中,我调用了监听器,这将触发工作适配器类:

And in my back button in my activity class, I call the listener, which will trigger the work in the adapter class:

    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                ArrayList<FoodType> foodTypes = backFilterEvent.doEvent(); }

这篇关于接口观察者模式:空对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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