我在使用EventBus时遇到了一些麻烦。 特别是当我试图运行应用程序时,它给了我错误
10-16 22:59:38.277: E/AndroidRuntime(1175): Caused by: de.greenrobot.event.
EventBusException: Subscriber class com.example.listtame.UI.TaskAdapter
has no public methods called onEvent
然而,它确实具有如下列出的公共方法。有谁知道为什么Eventbus会
抛出这个例外?
谢谢!
package com.example.listtame.UI;
import java.util.Collections;
import java.util.List;
import com.example.listtame.R;
import com.example.listtame.Activities.TaskList;
import com.example.listtame.CustomObj.Task;
import com.example.listtame.Events.TaskListFetchedEvent;
import de.greenrobot.event.EventBus;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class TaskAdapter extends BaseAdapter {
private List<Task> tasks = Collections.emptyList();
private final Context context;
public TaskAdapter(Context context) {
this.context = context;
EventBus.getDefault().register(this);
}
public void OnEventMainThread(TaskListFetchedEvent e) {
this.tasks = TaskListFetchedEvent.mTaskList;
notifyDataSetChanged();
}
private static class ViewHolder {
public final ImageView task_image;
public final TextView task_text;
public ViewHolder(ImageView task_image, TextView task_text) {
this.task_image = task_image;
this.task_text = task_text;
}
}
public void updateTasks(List<Task> tasks) {
this.tasks = tasks;
notifyDataSetChanged();
}
@Override
public int getCount() {
return tasks.size();
}
@Override
public Task getItem(int position) {
return tasks.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView task_image;
TextView task_text;
if (convertView == null) {
convertView = LayoutInflater.from(context)
.inflate(R.layout.task_box, parent, false);
task_image = (ImageView) convertView.findViewById(R.id.task_icon);
task_text = (TextView) convertView.findViewById(R.id.task_text);
convertView.setTag(new ViewHolder(task_image, task_text));
} else {
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
task_image = viewHolder.task_image;
task_text = viewHolder.task_text;
}
Task task = getItem(position);
task_text.setText(task.getText());
task_image.setImageResource(R.drawable.ic_debug);
return convertView;
}
}
我也尝试将OnEventMainThread方法更改为OnEvent。我有同样的例外情况。 如果有人好奇,可以在下面发布完整的LogCat。
10-16 22:59:38.261: D/AndroidRuntime(1175): Shutting down VM
10-16 22:59:38.261: W/dalvikvm(1175): threadid=1: thread exiting with uncaught exception (group=0xa62bb288)
10-16 22:59:38.277: D/dalvikvm(1175): GC_CONCURRENT freed 264K, 4% free 10863K/11207K, paused 12ms+1ms, total 16ms
10-16 22:59:38.277: E/AndroidRuntime(1175): FATAL EXCEPTION: main
10-16 22:59:38.277: E/AndroidRuntime(1175): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listtame/com.example.listtame.Activities.MainVerticalActivity}: android.view.InflateException: Binary XML file line #58: Error inflating class fragment
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.os.Looper.loop(Looper.java:137)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-16 22:59:38.277: E/AndroidRuntime(1175): at java.lang.reflect.Method.invokeNative(Native Method)
10-16 22:59:38.277: E/AndroidRuntime(1175): at java.lang.reflect.Method.invoke(Method.java:511)
10-16 22:59:38.277: E/AndroidRuntime(1175): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-16 22:59:38.277: E/AndroidRuntime(1175): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-16 22:59:38.277: E/AndroidRuntime(1175): at dalvik.system.NativeStart.main(Native Method)
10-16 22:59:38.277: E/AndroidRuntime(1175): Caused by: android.view.InflateException: Binary XML file line #58: Error inflating class fragment
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-16 22:59:38.277: E/AndroidRuntime(1175): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.app.Activity.setContentView(Activity.java:1867)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:217)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:77)
10-16 22:59:38.277: E/AndroidRuntime(1175): at com.example.listtame.Activities.MainVerticalActivity.onCreate(MainVerticalActivity.java:67)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.app.Activity.performCreate(Activity.java:5008)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-16 22:59:38.277: E/AndroidRuntime(1175): ... 11 more
10-16 22:59:38.277: E/AndroidRuntime(1175): Caused by: de.greenrobot.event.EventBusException: Subscriber class com.example.listtame.UI.TaskAdapter has no public methods called onEvent
10-16 22:59:38.277: E/AndroidRuntime(1175): at de.greenrobot.event.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:99)
10-16 22:59:38.277: E/AndroidRuntime(1175): at de.greenrobot.event.EventBus.register(EventBus.java:186)
10-16 22:59:38.277: E/AndroidRuntime(1175): at de.greenrobot.event.EventBus.register(EventBus.java:140)
10-16 22:59:38.277: E/AndroidRuntime(1175): at com.example.listtame.UI.TaskAdapter.<init>(TaskAdapter.java:33)
10-16 22:59:38.277: E/AndroidRuntime(1175): at com.example.listtame.Activities.TaskList.onCreate(TaskList.java:42)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.support.v4.app.Fragment.performCreate(Fragment.java:1481)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1099)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1201)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:292)
10-16 22:59:38.277: E/AndroidRuntime(1175): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
10-16 22:59:38.277: E/AndroidRuntime(1175): ... 24 more
EDIT ***********************************
迈克尔已经发布了以下解决方案。 onEvent需要是onEvent NOT OnEvent。答案 0 :(得分:1)
在浏览github上的greenrobot EventBus代码后,我发现他们希望您使用&#34; onEvent&#34;来声明您的事件回调。 +我看到你正在做的一个可选修饰符。
但是,你正在用资本开始你的方法&#39; O&#39;。将你的方法重命名为onEventMainThread,我打赌它会工作!