订阅者类及其超类没有使用@subscribe注释的公共方法

时间:2017-08-01 12:56:21

标签: java android android-fragments greenrobot-eventbus

我正在实现EventBus lib来传递并从任何类或片段中获取数据,并且还订阅了一种方法来获取即时更改的数据......但是我收到以下错误消息:

  

org.greenrobot.eventbus.EventBusException:订阅者类java.lang.Boolean及其超类没有@Subscribe注释的公共方法

我已将方法订阅为github showed here

代码段

public class ItemFragment extends Fragment {
    View view;
    String data;
    RecyclerView recyclerView;
    CategoryAdapter itemAdapter;
    List<Category.Items> list;
    private String TAG = getClass().getName();


    public static ItemFragment newInstance(String detail) {

        Bundle args = new Bundle();
        ItemFragment fragment = new ItemFragment();
        args.putString("data", detail);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.cat_recy, container, false);

        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        data = getArguments().getString("data");



    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getEventBusData(MessageEvent messageEvent) {
        if (messageEvent != null) {
            Category.Items itemClass = EventBus.getDefault().getStickyEvent(Category.Items.class);
            Log.e("eventBus", itemClass.getItem_name() + "  " + itemClass.getPrice() + "  " + itemClass.getCount());
        }

    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

我只发布了采用者所需的代码,因为这里不需要其余的代码

适配器

 @Override
        public void onBindViewHolder(final Holder holder, int position) {
            final Category.Items category = list.get(position);
      holder.catPlus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
     EventBus.getDefault().postSticky(category);
    }
    }

更新活动类

 @Override
protected void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this);
}

@Override
protected void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);

}
@Subscribe(threadMode = ThreadMode.MAIN)
public void getEventBusData(Items messageEvent) {
    if (messageEvent != null) {
        Items itemClass = EventBus.getDefault().getStickyEvent(Items.class);
        Log.e("eventBus", itemClass.getItem_name() + "  " + itemClass.getPrice() + "  " + itemClass.getCount());
        calculation();
    }

}

新错误

org.greenrobot.eventbus.EventBusException: Subscriber class com.icanstudioz.foodpaprica.fragment.ItemFragment already registered to event class com.icanstudioz.foodpaprica.data.Items

2 个答案:

答案 0 :(得分:3)

-keepattributes *Annotation*
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

答案 1 :(得分:0)

请通过以下代码更改onStartonDestroy代码:

@Override 
    public void onStart() { 
        super.onStart(); 
        EventBus.getDefault().register(this); 
    } 

    @Override 
    public void onDestroy() { 
        EventBus.getDefault().unregister(this); 
        super.onDestroy(); 
    } 

我正在做的是,将活动注册到当前班级。

有关详情,请参阅http://greenrobot.org/eventbus/documentation/how-to-get-started/