java.lang.RuntimeException:看起来你在 Android 上使用 EventBus,确保将 "eventbus" Android 库添加到你的依赖项中
java.lang.RuntimeException: It looks like you are using EventBus on Android, make sure to add the "eventbus" Android library to your dependencies
我是这个 eventbus 库的粉丝,并且在其他项目中很好地使用了它,没有遇到任何问题。
但是现在,我在 activity 上注册事件总线时遇到了一些奇怪的问题,并在这里卡住了这部分...
java.lang.RuntimeException:看起来你在 Android 上使用 EventBus,确保将“eventbus”Android 库添加到你的依赖。
我在 bluestack5 和模拟器上都遇到了这个错误...
我肯定已经正确安装了这个库,但遇到了这个奇怪的错误。有没有人以前遇到过类似的错误?谢谢。
============================================= ========
***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;
}
}
}
当您调用 register
时抛出此异常。这是代码的样子
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.");
}
这让我相信你有这种依赖性
implementation("org.greenrobot:eventbus-java:3.3.1")
而不是 Android
implementation("org.greenrobot:eventbus:3.3.1")
我是这个 eventbus 库的粉丝,并且在其他项目中很好地使用了它,没有遇到任何问题。 但是现在,我在 activity 上注册事件总线时遇到了一些奇怪的问题,并在这里卡住了这部分...
java.lang.RuntimeException:看起来你在 Android 上使用 EventBus,确保将“eventbus”Android 库添加到你的依赖。
我在 bluestack5 和模拟器上都遇到了这个错误...
我肯定已经正确安装了这个库,但遇到了这个奇怪的错误。有没有人以前遇到过类似的错误?谢谢。
============================================= ========
***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;
}
}
}
当您调用 register
时抛出此异常。这是代码的样子
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.");
}
这让我相信你有这种依赖性
implementation("org.greenrobot:eventbus-java:3.3.1")
而不是 Android
implementation("org.greenrobot:eventbus:3.3.1")