Skip to content
Advertisement

java.lang.RuntimeException: It looks like you are using EventBus on Android, make sure to add the “eventbus” Android library to your dependencies

I am a fan of this eventbus library and used that on the other projects well without getting any issues. But now, I am getting some odd issue with registering eventbus on the activity and got stuck with this part here…

java.lang.RuntimeException: It looks like you are using EventBus on Android, make sure to add the “eventbus” Android library to your dependencies.

I am getting this error on both – bluestack5 and emulators…

I’ve installed the library correctly for sure and getting this odd error. Is there any one who got the similar error before? Thank you.

=====================================================

***java.lang.RuntimeException: It looks like you are using EventBus on Android, make sure to add the "eventbus" Android library to your dependencies.
        at org.greenrobot.eventbus.EventBus.register(EventBus.java:145)
        at com.abc.auth.TutorialActivity.onStart(TutorialActivity.java:45)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1425)**
        at android.app.Activity.performStart(Activity.java:7825)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3294)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)*

===========================================================

package com.abc.auth;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;

import com.abc.databinding.ActivityTutorialBinding;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class TutorialActivity extends AppCompatActivity {
    private ActivityTutorialBinding binding;

    private static final int NUM_PAGES = 3;
    private ViewPager2 mPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityTutorialBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);

        initComponents();
    }

    void initComponents(){
        mPager = binding.pagerTutorial;
        ScreenSlidePagerAdapter pagerAdapter = new ScreenSlidePagerAdapter(this);
        mPager.setAdapter(pagerAdapter);
    }

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

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

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        if (mPager.getCurrentItem() == 2){
            finish();
        }
        else{
            mPager.setCurrentItem(mPager.getCurrentItem() + 1);
        }
    }

    @Override
    public void onBackPressed() {
        if (mPager.getCurrentItem() == 0) {
            super.onBackPressed();
        } else {
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        }
    }

    private static class ScreenSlidePagerAdapter extends FragmentStateAdapter {
        public ScreenSlidePagerAdapter(@NonNull AppCompatActivity fragmentActivity) {
            super((FragmentActivity) fragmentActivity);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            TutorialSlideFragment fragment = new TutorialSlideFragment();
            Bundle args = new Bundle();
            args.putInt("tutorialIndex", position);
            fragment.setArguments(args);

            return fragment;
        }

        @Override
        public int getItemCount() {
            return NUM_PAGES;
        }
    }

    public static class MessageEvent {
        public String msg;

        public MessageEvent(String msg) {
            this.msg = msg;
        }
    }
}

Advertisement

Answer

This exception is thrown when you call register. Here is what the code looks like

    if (AndroidDependenciesDetector.isAndroidSDKAvailable() && !AndroidDependenciesDetector.areAndroidComponentsAvailable()) {
        // Crash if the user (developer) has not imported the Android compatibility library.
        throw new RuntimeException("It looks like you are using EventBus on Android, " +
                "make sure to add the "eventbus" Android library to your dependencies.");
    }

This leads me to believe you have this dependency

implementation("org.greenrobot:eventbus-java:3.3.1")

instead of this which is for Android

implementation("org.greenrobot:eventbus:3.3.1")

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement