Greenrobot EventBus Could Not Dispatch Event

JuddGledhill picture JuddGledhill · Oct 15, 2014 · Viewed 9.9k times · Source

I am using EventBus to communicate from a long running thread to Fragments that update their UI. This long running thread is basically listening for network activity, parsing it and putting events on the bus. I have 2 Fragments set to receive the events.

I am getting a error when I update the Fragments' UIs (a textview) from the Fragments' onEvent method. How do I update the UI in a Fragment from a separate thread?

Here is my code for the Fragments and the logcat output I am seeing...

Logcat:

10-15 02:19:44.637: I/System.out(682): debugger has settled (1440)
10-15 02:19:45.021: V/BRRT(682): start thread
10-15 02:19:45.021: V/BRRT(682): past start thread
10-15 02:19:45.031: V/TEST(682): running thread...
10-15 02:19:45.121: D/Event(682): No subscribers registered for event class com.broadreach.radiotester.io.ExpeditionEvent
10-15 02:19:45.121: D/Event(682): No subscribers registered for event class de.greenrobot.event.NoSubscriberEvent
10-15 02:19:45.261: V/BRRT(682): OneUp event received: 204 : 32
10-15 02:19:45.271: W/ResourceType(682): No package identifier when getting value for resource number 0x00000000
10-15 02:19:45.351: E/Event(682): Could not dispatch event: class com.broadreach.radiotester.io.ExpeditionEvent to subscribing class class com.broadreach.radiotester.screens.OneUpScreen
10-15 02:19:45.351: E/Event(682): android.content.res.Resources$NotFoundException: String resource ID #0x0
10-15 02:19:45.351: E/Event(682):   at android.content.res.Resources.getText(Resources.java:247)
10-15 02:19:45.351: E/Event(682):   at android.widget.TextView.setText(TextView.java:3428)
10-15 02:19:45.351: E/Event(682):   at com.broadreach.radiotester.screens.OneUpScreen.onEvent(OneUpScreen.java:47)
10-15 02:19:45.351: E/Event(682):   at java.lang.reflect.Method.invokeNative(Native Method)
10-15 02:19:45.351: E/Event(682):   at java.lang.reflect.Method.invoke(Method.java:511)
10-15 02:19:45.351: E/Event(682):   at de.greenrobot.event.EventBus.invokeSubscriber(EventBus.java:569)
10-15 02:19:45.351: E/Event(682):   at de.greenrobot.event.EventBus.postToSubscription(EventBus.java:500)
10-15 02:19:45.351: E/Event(682):   at de.greenrobot.event.EventBus.subscribe(EventBus.java:288)
10-15 02:19:45.351: E/Event(682):   at de.greenrobot.event.EventBus.register(EventBus.java:189)
10-15 02:19:45.351: E/Event(682):   at de.greenrobot.event.EventBus.registerSticky(EventBus.java:166)
10-15 02:19:45.351: E/Event(682):   at com.broadreach.radiotester.screens.OneUpScreen.onResume(OneUpScreen.java:33)
10-15 02:19:45.351: E/Event(682):   at android.support.v4.app.Fragment.performResume(Fragment.java:1547)
10-15 02:19:45.351: E/Event(682):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:978)
10-15 02:19:45.351: E/Event(682):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
10-15 02:19:45.351: E/Event(682):   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-15 02:19:45.351: E/Event(682):   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
10-15 02:19:45.351: E/Event(682):   at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
10-15 02:19:45.351: E/Event(682):   at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
10-15 02:19:45.351: E/Event(682):   at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
10-15 02:19:45.351: E/Event(682):   at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
10-15 02:19:45.351: E/Event(682):   at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
10-15 02:19:45.351: E/Event(682):   at android.view.View.measure(View.java:12603)
10-15 02:19:45.351: E/Event(682):   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
10-15 02:19:45.351: E/Event(682):   at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
10-15 02:19:45.351: E/Event(682):   at android.view.View.measure(View.java:12603)
10-15 02:19:45.351: E/Event(682):   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
10-15 02:19:45.351: E/Event(682):   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
10-15 02:19:45.351: E/Event(682):   at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
10-15 02:19:45.351: E/Event(682):   at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
10-15 02:19:45.351: E/Event(682):   at android.view.View.measure(View.java:12603)
10-15 02:19:45.351: E/Event(682):   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
10-15 02:19:45.351: E/Event(682):   at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
10-15 02:19:45.351: E/Event(682):   at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2072)
10-15 02:19:45.351: E/Event(682):   at android.view.View.measure(View.java:12603)
10-15 02:19:45.351: E/Event(682):   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1044)
10-15 02:19:45.351: E/Event(682):   at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
10-15 02:19:45.351: E/Event(682):   at android.os.Handler.dispatchMessage(Handler.java:99)
10-15 02:19:45.351: E/Event(682):   at android.os.Looper.loop(Looper.java:137)
10-15 02:19:45.351: E/Event(682):   at android.app.ActivityThread.main(ActivityThread.java:4340)
10-15 02:19:45.351: E/Event(682):   at java.lang.reflect.Method.invokeNative(Native Method)
10-15 02:19:45.351: E/Event(682):   at java.lang.reflect.Method.invoke(Method.java:511)
10-15 02:19:45.351: E/Event(682):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-15 02:19:45.351: E/Event(682):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-15 02:19:45.351: E/Event(682):   at dalvik.system.NativeStart.main(Native Method)
10-15 02:19:45.351: D/Event(682): No subscribers registered for event class de.greenrobot.event.SubscriberExceptionEvent
10-15 02:19:45.551: V/BRRT(682): FourUp event received: 204 : 32
10-15 02:19:45.561: W/ResourceType(682): No package identifier when getting value for resource number 0x00000000

Here is one of the Fragments (they look the same):

public class OneUpScreen extends ScreenFragment {   
    private Button name;
    private TextView c00Title, c00Data;

    //DEBUG
    public String title;

    //DEBUG
    private int counter = 0;

    //for logging and convenience
    private static final String TAG = "BRRT";   


    @Override
    public void onResume(){
        super.onResume();
        EventBus.getDefault().registerSticky(this);
    }

    @Override
    public void onPause(){
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    //process the bus messaging
    public void onEvent(ExpeditionEvent event){
        Log.v(TAG, "OneUp event received: " + event.getEventId() + " : " + event.getEventScreenValue());

        c00Data = (TextView) getView().findViewById(R.id.c00Data);
        c00Data.setText(counter);

        counter++;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.one_up_screen_layout, container, false);

        name = (Button) v.findViewById(R.id.lblTitle);
        c00Title = (TextView) v.findViewById(R.id.c00Title);
        c00Data = (TextView) v.findViewById(R.id.c00Data);

        Bundle bundle = this.getArguments();

        //initial set up of the text on the screen
        //null check
        name.setText((bundle.getString("name") == null) ? "error" : bundle.getString("name"));
        c00Title.setText((bundle.getString("c00Title") == null) ? "error" : bundle.getString("c00Title"));
        title = bundle.getString("c00Title");

        return v;
    }

    public OneUpScreen(){

    }

    public static OneUpScreen newInstance() {
        OneUpScreen frag = new OneUpScreen();
        return frag;
    }

    //attach the config for this instance.
    @Override
    public void setConfig(ScreenConfig sc){
        //set up the data to paint the screen for the first time
        Bundle b = new Bundle();

        //now we have to parse some stuff into a bundle and send the bundle to the fragment
        b.putString("name", sc.getName());
        b.putString("c00Title", sc.getEvents().get(0).getCleanName());
        b.putInt("c00Data", sc.getEvents().get(0).getEventID());

        //pass it along
        this.setArguments(b);
    }


    @Override
    public void update(SparseArray<String> sensorDataMap) {
        Log.v(TAG, "OneUp Update called: " + counter);
        counter++;
    }
}

The onEvent method is where I am looking here. If you notice in the log, that method does get invoked. But, I am unable to hit the textView inside of it. Originally I was just holding the reference to the textview that I take in the onCreate() method. but I saw and example where you look it up again in the event processing so I moved to that, no luck.

If I take out the:

c00Data.setText(counter);

then everthing works well.

What am I missing here?

Answer

JuddGledhill picture JuddGledhill · Oct 15, 2014

Ok, for anyone who comes here later, here is how I solved this...

It turns out that the key error I was seeing was not the event related error, but the resource related one.

10-15 02:19:45.271: W/ResourceType(682): No package identifier when getting value for resource number 0x00000000

I looked elsewhere on SO and found this:

No package identifier when getting value for resource number

Then I went back into my code and rewrote the TextView.setText() calls to look different.

Old way (counter is an int):

c00Data.setText(counter);

New way:

c00Data.setText(""+counter);

That did it. I whooped.