0

我在使用 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。Etiher方式我得到同样的例外。如果有人好奇,请在下面发布完整的 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

编辑***********************************

迈克尔在下面发布了解决方案。onEvent 必须是 onEvent 而不是 OnEvent。

4

1 回答 1

1

在查看了 github 上的 greenrobot EventBus 代码后,我看到他们希望您使用“onEvent”+ 一个可选修饰符来声明您的事件回调,我看到您正在这样做。

但是,您的方法以大写“O”开头。将您的方法重命名为 onEventMainThread,我敢打赌它会起作用!

于 2014-10-16T23:15:50.200 回答