question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Silencing exception on registered object without onEvent method

See original GitHub issue

I have a BaseActivity class which acts as base for all Android activities in my project.

EventBus.register(this) is done in the BaseActivity without any onEvent methods in it. Some of its subclasses have onEvent and some don’t. I got this exception when starting activities (subclass of BaseActivity) without onEvent methods:

Subscriber class com.example.LoginActivity has no public methods called onEvent  

That’s obviously because LoginActivity has been registered to EventBus in the super class but has no onEvent method. However I could get around this problem by putting empty onEvent in the BaseActivity (a little bit unclean solution)

I suggest that no exception should be thrown in that case (no onEvent methods). Let me know what you think.

Thanks

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
BrainKucommented, Aug 12, 2014

I think it’d better not to use try-catch here, it’s unnecessary. I prefer provide a value in BaseActivity to indicate if the child need register an event;

public class BaseActivity extends Activity {

    private boolean isNeedRegister = false;

    protected void setNeedRegister() {
        this.isNeedRegister = true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isNeedRegister) {
            EventBus.getDefault().register(this);
        }
    }
}

If ChildActivity need register, just use like this

public class ChildActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setNeedRegister();
      ... ...
    }
}

0reactions
PratikPagadacommented, Jul 17, 2017

I’ve got same issue while working with activity.

Subscriber class has no public methods called onEvent

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chapter 20: Exception Handling Frameworks - Micro Focus
The "raiseException" method is implemented in the Base class and is inherited by all subclasses. If there is no exception handler registered for...
Read more >
event loop processing model - HTML - WhatWG
Event handlers, whether registered through the DOM using addEventListener() , by explicit event handler content attributes, by event handler ...
Read more >
Promise.prototype.catch() - JavaScript - MDN Web Docs
The catch() method of a Promise object schedules a function to be called when the promise is rejected.
Read more >
NullReferenceException when triggering event - Stack Overflow
You need a null check - in C# you can't call events when there are no handlers registered on that event.
Read more >
Exception Handling | RESTful Java with JAX-RS 2 ... - dennis-xlc
It will continue this process until there are no more superclasses to match against. Finally, ExceptionMappers are registered with the JAX-RS runtime using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found