2014-11-05 54 views
3

我正在爲小應用程序設計UnitTest。這個應用程序cout你使用的是出生日期。我有一個問題,我調用方法檢查數字並將值放入TextView。我得到了一個錯誤Android:只有創建視圖層次結構的原始線程可以觸及其視圖 - UnitTest

`android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6347) 
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:871) 
at android.view.View.requestLayout(View.java:16472) 
at android.view.View.requestLayout(View.java:16472) 
at android.view.View.requestLayout(View.java:16472) 
at android.view.View.requestLayout(View.java:16472) 
at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352) 
at android.view.View.requestLayout(View.java:16472) 
at android.widget.ScrollView.requestLayout(ScrollView.java:1481) 
at android.view.View.requestLayout(View.java:16472) 
at android.view.View.requestLayout(View.java:16472) 
at android.widget.TextView.checkForRelayout(TextView.java:6817) 
at android.widget.TextView.setText(TextView.java:3947) 
at android.widget.TextView.setText(TextView.java:3805) 
at android.widget.TextView.setText(TextView.java:3780) 
at com.numerology.MainActivity.showTextInfo(MainActivity.java:168) 
at com.numerology.test.Numerology.testMasterNumber(Numerology.java:46) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214) 
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199) 
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192) 
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) 
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176) 
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554) 
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701) 

`

我的佈局: `

<TextView 
    android:id="@+id/lbInfo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:textColor="@color/yellow" 
    android:text="Please set the date of Your birth" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/lblDate" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="80dp" 
    android:textColor="@color/white" 
    android:text="Date of your birth (DD-MM-YYYY): " 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/tvDate" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="100dp" 
    android:textColor="@color/white" 
    android:text="" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
    android:id="@+id/numberInfo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="130dp" 
    android:textColor="@color/yellow" 
    android:text="" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
    android:id="@+id/description" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="160dp" 
    android:textColor="@color/yellow" 
    android:text="Description:" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 


<DatePicker 
    android:id="@+id/dpResult" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/tvDate" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="88dp" /> 

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_marginTop="30dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/description"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
<TextView 
     android:id="@+id/allInformation" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/white" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceMedium" />   
</LinearLayout> 
</ScrollView> 

</RelativeLayout>` 

我的方法,我想測試: `

public void showTextInfo(int number){ 
     if (number == 1){ 
      finInformation.setText("People with vibration digits 1, are born leaders, who can  not stand authority over him and wishing at all costs to preserve their independence.  Confident, blockbuster, effective in action, full of charisma, have a really high potential  energy and a sense of independence. They feel a strong need for self-improvement. They are  very creative, able to demonstrate their own initiative, they have no problems with  communicative, able to speak beautifully. Top comes the realization of short-term actions.  \n\n" + 
        "Strength of character, individuality, desire to create, creativity, dominance, bold ideas, persuasive, charisma and outstanding leadership skills to help them in reconciliation of people, which ones indicate the way forward. Ones usually have high authority and are often the model to follow. " + 
        "They have a practical sense, a strong will, organizational capacity, but smaller executive ability. These are dynamic in nature, characterized by their spontaneity, willingness to take risks. Numerological ambitious ones are those having the ability to self-realization and organizational talent. They are enterprising and intelligent, feel a high need for power, achievement of high social position. " + 
        "Ones strive for perfection, are self-critical. Not tolerate criticism from others. Their weaknesses, it peremptoriness, a tendency to self-absorption, domination, arrogance, risky behavior, the tendency to impose their own opinions, impatience. \n\n" + 
        "Failures can cause a feeling of frustration, excessive nervousness and explosiveness. In partnership readily show their feelings, they feel a great need to appreciate, may be overly jealous, even possessive. " + 
        "For people with One numerology number it's hard to get a compromise, they prefer to impose their views and way of seeing."); 
     }` 
+1

由於錯誤說你正在訪問ui線程從非ui線程 – 2014-11-05 10:23:10

回答

0

把代碼在新線程中將值設置爲textview的位置;

Handler h=new Handler(); 
    h.post(new Runnable() { 
     public void run() { 

     textView.setText(value); 
     } 
    }); 

看起來你的textview是finInformation在run()中使用;

+0

我解決了這個問題之前,我的測試方法之前使用註釋@UiThreadTest。感謝您的快速回答:) – user3388384 2014-11-05 10:24:20

7

我在我的測試方法之前用@iThreadTest解決了這個問題。

0

getActivity()實現如下:

​​

從什麼時候開始getActivity()回報活動實例,這意味着,它必須創建活動。當Activity被創建時,它必須創建Activity的View。活動的視圖只能通過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 */ 
    } 

現在,每個測試用例UI線程的記憶裏。因此,您應該明確使用@UiThreadTest進行每項測試。

相關問題