EventBus - Subscriber class and its super classes have no public methods with the @subscribe annotation

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

I'm creating an Android application using EventBus for posting asynchronous broadcasts to other classes, but I'm running into an error during execution.

MainActivity.java

import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import com.google.android.gms.maps.model.LatLng; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode;   public class MainActivity extends AppCompatActivity {      //Globals     public String uname = null;     public double lat = 0;     public double lng = 0;      //Get GUI handles     public Button sendButton; //     public EditText username;     public Button MapButton; //     public EditText LatBox;     public EditText LngBox;       protected void onDestroy() {         super.onDestroy();         EventBus.getDefault().unregister(this);     }      @Override     protected void onCreate(Bundle savedInstanceState) {          //register EventBus         EventBus.getDefault().register(this);          super.onCreate(savedInstanceState);         //set GUI for MainActivity         setContentView(R.layout.activity_main);          //get handlers         LatBox = (EditText)findViewById(R.id.LatBox);         LngBox = (EditText)findViewById(R.id.LngBox);          MapButton = (Button)findViewById(R.id.locationButton);         //Call the class which will handle finding coordinates         MapButton.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 Intent MapIntent = new Intent(getApplicationContext(), MapClass.class);                 startActivityForResult(MapIntent, 0);             }         });          sendButton = (Button)findViewById(R.id.Submit);         //Set action for Button         sendButton.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                  //Get username from user                 username  = (EditText)findViewById(R.id.UsernameText);                 uname = username.getText().toString();                  //Generate intent to start IntentService                 Intent i = new Intent(getApplicationContext(), Register.class);                  //Put the extra field of username                 i.putExtra("username", uname);                 i.putExtra("latitude", lat);                 i.putExtra("longitude", lng);                 i.putExtra("type", "meetup.be2015.gcm_meetup.MAIN_ACTIVITY");                  //Start the IntentService on a different thread                 startService(i);             }         });      }       @Subscribe(threadMode = ThreadMode.MAIN)     public void onEvent(LatLng currentPos){          LatBox.setText(String.valueOf(currentPos.latitude));         LngBox.setText(String.valueOf(currentPos.longitude));          lat = currentPos.latitude;         lng = currentPos.longitude;     } } 

MapClass.java

import android.app.IntentService; import android.content.Intent; import android.location.Location; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.location.LocationServices; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.model.LatLng; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe;  public class MapClass extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {      private GoogleApiClient mGoogleApiClient;     private GoogleMap mgoogleMap;     private LatLng latLng;     private GoogleApiClient client;      @Override     public void onMapReady(GoogleMap googleMap) {         mgoogleMap = googleMap;         mgoogleMap.setMyLocationEnabled(true);      //Sets location to current position         buildGoogleApiClient();         mGoogleApiClient.connect();     }      protected synchronized void buildGoogleApiClient() {         mGoogleApiClient = new GoogleApiClient.Builder(this)                 .addConnectionCallbacks(this)                 .addOnConnectionFailedListener(this)                 .addApi(LocationServices.API)                 .build();     }      @Override     public void onDestroy() {         super.onDestroy();         if (EventBus.getDefault().isRegistered(this)) {             EventBus.getDefault().unregister(this);         }     }      @Override     public void onConnected(Bundle bundle) {         Location MLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);         if (MLastLocation != null) {             latLng = new LatLng(MLastLocation.getLatitude(), MLastLocation.getLongitude());              //Post the LatLng to MainActivity             EventBus.getDefault().post(latLng);              //Send sticky event to Register and MyGcmListenerService             EventBus.getDefault().postSticky(latLng);          } else {             Log.d("onConnected", "Value of LatLng is NULL");             latLng = new LatLng(0, 0);   //equator         }     }      @Override     public void onConnectionSuspended(int i) {         //Notify         Log.d("ConnectionSuspended", "Connection Suspended. Status: " +   i);         mgoogleMap.clear();         mGoogleApiClient.disconnect();     }      @Override     public void onConnectionFailed(ConnectionResult connectionResult) {         //Notify         Log.d("ConnectionFailed", "Connection Failed. Status: " + connectionResult.toString());         mgoogleMap.clear();         mGoogleApiClient.disconnect();     }      @Subscribe     public void onEvent() {         Log.d("EVENT", "EVENT");     }      @Override     public void onStart() {         super.onStart();         if (!EventBus.getDefault().isRegistered(this)) {             EventBus.getDefault().register(this);         }       @Override     public void onStop() {         super.onStop();         if (EventBus.getDefault().isRegistered(this)) {             EventBus.getDefault().unregister(this);         }      } } 

The LogCat shows the following:

Why is this happening? Am I doing something wrong?

回答1:

i think it is because onEvent inside MapClass.java has no parameter. Could you try with the expected parameter?



回答2:

Please ensure these lines are in your proguard config file if you are using proguard for your builds.

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


回答3:

In my situation,I got this error for I did't write @Subscribe on the class where i register EventBus.



回答4:

I faced the same issue and after a long research got the solution for every case. This problem is due to absence of @Subscribe public method onEvent() inside the class which you are trying to register Event bus as EventBus.getDefault().register(this). Presence of this function is mandatory if you register a class with Event bus

This can be in two situations

  1. using progruad : progruad may modify name of method onEvent() due to which event bus is not able to find it. Put these lines inide your progruad rules

    -keepattributes Annotation

    -keepclassmembers class ** {

    @org.greenrobot.eventbus.Subscribe ;

    }

    -keep enum org.greenrobot.eventbus.ThreadMode { *;

}

  1. if you are not using progruard then definitely your class is missing the method onEvent() with @Subscribe annotation. This annotation with method is mandatory with EventBus version 3.0.0 so double check presence of this method inside your class.


回答5:

Just in case your code is like mine :p

I had to set the method as public because it's currently private.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!