3

我正在使用 EventBus 从长时间运行的线程与更新其 UI 的片段进行通信。这个长时间运行的线程基本上是在监听网络活动,解析它并将事件放在总线上。我设置了 2 个片段来接收事件。

从 Fragments 的 onEvent 方法更新 Fragments 的 UI(文本视图)时出现错误。如何从单独的线程更新片段中的 UI?

这是我看到的片段和 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);

然后一切正常。

我在这里想念什么?

4

1 回答 1

7

好的,对于以后来这里的任何人,这就是我解决这个问题的方法......

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

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

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

获取资源编号值时没有包标识符

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

老方法(计数器是一个整数):

c00Data.setText(counter);

新的方法:

c00Data.setText(""+counter);

做到了。我叫了一声。

于 2014-10-15T21:09:21.740 回答