编程语言
首页 > 编程语言> > java – Greenrobot EventBus无法发送事件

java – Greenrobot EventBus无法发送事件

作者:互联网

我正在使用EventBus从长时间运行的线程传递到更新其UI的Fragments.这个长时间运行的线程基本上是在监听网络活动,解析它并将事件放在总线上.我有2个片段设置接收事件.

当我从Fragments的onEvent方法更新Fragments的UI(textview)时出现错误.如何从单独的线程更新片段中的UI?

这是我的片段代码和我看到的logcat输出…

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

这是一个碎片(它们看起来一样):

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++;
    }
}

onEvent方法是我在这里看的地方.如果您在日志中注意到,则会调用该方法.但是,我无法点击它内部的textView.最初我只是对onCreate()方法中的textview进行了引用.但是我看到了一个例子,你在事件处理中再次查看它,所以我转向那个,没有运气.

如果我拿出:

c00Data.setText(counter);

那么外翻效果很好.

我在这里错过了什么?

解决方法:

好的,对于后来这里的人来说,这就是我解决这个问题的方法……

事实证明,我看到的关键错误不是与事件相关的错误,而是与资源相关的错误.

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

我在其他地方看了SO并发现了这个:

No package identifier when getting value for resource number

然后我回到我的代码并重写TextView.setText()调用看起来不同.

旧方式(计数器是一个int):

c00Data.setText(counter);

新方法:

c00Data.setText(""+counter);

这样做了.我呐喊.

标签:android,java,android-fragments,multithreading,greenrobot-eventbus
来源: https://codeday.me/bug/20190728/1560972.html