Android: Only the original thread that created a view hierarchy can touch its views - UnitTest

前端 未结 4 1022
别跟我提以往
别跟我提以往 2021-01-13 02:03

I\'m designing UnitTest for little app. This app cout what numerology number You are using birth date. I have a problem, I what to invoke method which check number and put v

相关标签:
4条回答
  • 2021-01-13 02:33

    For React Native close performance monitor

    0 讨论(0)
  • 2021-01-13 02:36

    getActivity() implementation is as below:

       /**
             * @return The activity under test.
             */
            public T getActivity() {
                if (mActivity == null) {
                    Log.w(TAG, "Activity wasn't created yet");
                }
                return mActivity;
            }
    

    Since when getActivity() returns Activity instance, it means, it must create Activity. When Activity gets created, it must create Activity's View. Activity's View can only be created via UI Thread.

    @Before
        public void setUp() throws Exception {
            mainActivity = intentsTestRule.getActivity(); / *now Activity's view gets created */
            /* Because Activity's view creation happens in UI Thread, so all the test cases, here, are used by UI Thread */
        }
    

    Now, every test cases are inside the memory of UI Thread. Hence, you should explicitly use @UiThreadTest for every test.

    0 讨论(0)
  • 2021-01-13 02:38

    I solved this problem with adnotation @UiThreadTest before my test method.

    0 讨论(0)
  • 2021-01-13 02:41

    put the code where you set value to textview in a new thread;

      Handler h=new Handler();
      h.post(new Runnable() {
            public void run() {
    
            textView.setText(value);
            }
       });
    

    Looks lile your textview is finInformation use that in run();

    0 讨论(0)
提交回复
热议问题