org greenrobot eventbus android event-bus greenrobot-eventbus

android - greenrobot - EventBus-La clase de suscriptor y sus súper clases no tienen métodos públicos con la anotación @subscribe



eventbus android gradle (8)

Asegúrese de que estas líneas estén en el archivo de configuración de su programa si está utilizando el programa para sus compilaciones.

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

Estoy creando una aplicación de Android utilizando EventBus para publicar difusiones asíncronas en otras clases, pero me estoy encontrando con un error durante la ejecución.

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); } } }

El LogCat muestra lo siguiente:

03-08 22:54:56.970 8570-8570/meetup.be2015.gcm_meetup E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{meetup.be2015.gcm_meetup/meetup.be2015.gcm_meetup.MapClass}: org.greenrobot.eventbus.EventBusException: Subscriber class meetup.be2015.gcm_meetup.MapClass and its super classes have no public methods with the @Subscribe annotation at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2118) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2143) at android.app.ActivityThread.access$700(ActivityThread.java:140) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:174) at android.app.ActivityThread.main(ActivityThread.java:4952) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) at dalvik.system.NativeStart.main(Native Method) Caused by: org.greenrobot.eventbus.EventBusException: Subscriber class meetup.be2015.gcm_meetup.MapClass and its super classes have no public methods with the @Subscribe annotation at org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:67) at org.greenrobot.eventbus.EventBus.register(EventBus.java:136) at meetup.be2015.gcm_meetup.MapClass.onStart(MapClass.java:91) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178) at android.app.Activity.performStart(Activity.java:5198) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2091) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2143)  at android.app.ActivityThread.access$700(ActivityThread.java:140)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:174)  at android.app.ActivityThread.main(ActivityThread.java:4952)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:511)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)  at dalvik.system.NativeStart.main(Native Method) 

¿Por qué está pasando esto? ¿Estoy haciendo algo mal?


Creo que es porque onEvent dentro de MapClass.java no tiene ningún parámetro. ¿Podrías probar con el parámetro esperado?


En caso de que ayude a alguien, en mi caso, olvidé pasar argumentos al método de recepción, todo lo demás estaba bien. Cuando no se pasa ningún argumento a la función / método de recepción, en ese caso se lanza esta excepción.


En caso de que tu código sea como el mío: p

Tuve que establecer el método como public porque actualmente es private .


En mi caso, onEvent() era privado y colocado en clase infantil .

Pero register() y unregister() fueron llamados en la clase padre .

La solución fue hacer público el onEvent() .


En mi situación, recibí este error porque no escribí @Subscribe en la clase en la que registro EventBus.


En una nota al margen, recibí el mismo error después de cambiar la implementación de EventBus de Google a esta. Este error me volvió loco, porque el EventBus de Google también tiene una anotación @Subscribe y estaba usando esa en lugar de la que proporcionó greenrobot.

Está bien, es un error muy tonto de mi parte, pero si puedo ayudar incluso a una persona como yo, estoy feliz.


Me enfrenté al mismo problema y, después de una larga investigación, obtuve la solución para cada caso. Este problema se debe a la ausencia del método público @Subscribe onEvent () dentro de la clase en la que está intentando registrar el bus de eventos como EventBus.getDefault().register(this) . La presencia de esta función es obligatoria si registra una clase con el bus de eventos

Esto puede ser en dos situaciones.

  1. utilizando progruad: progruad puede modificar el nombre del método onEvent () debido a que el bus de eventos no puede encontrarlo. Ponga estas líneas dentro de las reglas de su programa.

    -tenerattributos Anotación

    -clase de los miembros de clase ** {

    @ org.greenrobot.eventbus.Subscribe;

    }

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

}

  1. Si no está utilizando progruard, definitivamente a su clase le falta el método onEvent () con la anotación @Subscribe. Esta anotación con el método es obligatoria con la versión 3.0.0 de EventBus, así que compruebe la presencia de este método dentro de su clase.