相信很多小伙伴在安卓开发中都用到了eventbus。
有没有遇到坑呢?
Subscriber class *** and its super classes have no public methods with the @Subscribe annotation
在实际使用过程中,注册之后,会报找不到公共的注解方法,需要在注册的地方 实现一个公共注解方法
package com.skyworth.tenant.ui.base;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.skyworth.tenant.loadsircallback.EmptyCallback;
import com.skyworth.tenant.loadsircallback.ErrorCallback;
import com.skyworth.tenant.loadsircallback.LoadingCallback;
import com.skyworth.tenant.receiver.NetworkStatusReceiver;
import com.skyworth.tenant.utils.EditTextUtil;
import com.skyworth.tenant.utils.EventBusTag;
import com.skyworth.tenant.utils.LogUtils;
import com.skyworth.tenant.utils.SignoutUtils;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import butterknife.ButterKnife;
import butterknife.Unbinder;
/**
@author: xulei
@desc:抽象activity 子类需要继承
@date: 2020/3/2
*/
public abstract class BaseActivity extends AppCompatActivity {
private Unbinder butterknife;
public LoadService loadService;
@SuppressLint(“SourceLockedOrientationActivity”)
@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//将所有activity实例放入,以便于退出时统一关闭
SignoutUtils.getInstance().addActivity(this);
initView();
initData();
//实现状态栏图标和文字颜色为暗色
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//禁止横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
LogUtils.e(LogUtils.tag,"activity类名: " + ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1).get(0).topActivity.getClassName() + "");
}
/**
加载布局/**
加载数据/**
点击加载,loadsir空页面等 可以重写实现刷新逻辑 @param v@Override
public void setContentView(View view) {
super.setContentView(view);
initEvent();
}
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
initEvent();
}
@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
super.setContentView(view, params);
initEvent();
}
//初始化一些事件。注解、eventbus…/
private void initEvent(){
butterknife = ButterKnife.bind(this);
//订阅事件注册
EventBus.getDefault().register(this);
loadService = LoadSir.getDefault().register(this, new Callback.OnReloadListener() {
@Override
public void onReload(View v) {
onNetReload(v);
}
});
loadService.showSuccess();
}
@Override
protected void onDestroy() {
super.onDestroy();
SignoutUtils.getInstance().destroy(this);
butterknife.unbind();
EventBus.getDefault().unregister(this);
}
//必须定义一个方法 否则 EventBusException: Subscriber class *** and its super classes have no public methods with the @Subscribe annotation
@Subscribe(threadMode = ThreadMode.MAIN)
public void OnEventMainThread(EventBusTag messageEvent) {
if (messageEvent!=null && !TextUtils.isEmpty(messageEvent.NETWORK)){
if (messageEvent.NETWORK.equals(“wifi”)){
}else if (messageEvent.NETWORK.equals("error")){
// loadService.showCallback(ErrorCallback.class);
}else {
}
}
}
//软键盘点击其他区域消失
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_UP) {
View v = getCurrentFocus();
if (EditTextUtil.isShouldHideKeyboard(v, ev)) {
EditTextUtil.hideKeyboard(this, v.getWindowToken());
// EventBus.getDefault().post("hide","hidekeyboard");
}
}
return super.dispatchTouchEvent(ev);
}
}
//必须定义一个方法 否则 EventBusException: Subscriber class *** and its super classes have no public methods with the @Subscribe annotation
@Subscribe(threadMode = ThreadMode.MAIN)
public void OnEventMainThread(EventBusTag messageEvent) {
}
EventBusTag 是自己定义的对象,里面存放了一些常用的值。