安卓:只有创建视图层次可以触摸其观点原来的线程 - 的UnitTest [英] Android: Only the original thread that created a view hierarchy can touch its views - UnitTest

查看:133
本文介绍了安卓:只有创建视图层次可以触摸其观点原来的线程 - 的UnitTest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计的UnitTest的小应用程序。这个程序清点什么命理数量正在使用的出生日期。我有一个问题,我该怎么调用方法,检查数量,并把价值成的TextView。我得到了一个错误

 `android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程。
在android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6347)
在android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:871)
在android.view.View.requestLayout(View.java:16472)
在android.view.View.requestLayout(View.java:16472)
在android.view.View.requestLayout(View.java:16472)
在android.view.View.requestLayout(View.java:16472)
在android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
在android.view.View.requestLayout(View.java:16472)
在android.widget.ScrollView.requestLayout(ScrollView.java:1481)
在android.view.View.requestLayout(View.java:16472)
在android.view.View.requestLayout(View.java:16472)
在android.widget.TextView.checkForRelayout(TextView.java:6817)
在android.widget.TextView.setText(TextView.java:3947)
在android.widget.TextView.setText(TextView.java:3805)
在android.widget.TextView.setText(TextView.java:3780)
在com.numerology.MainActivity.showTextInfo(MainActivity.java:168)
在com.numerology.test.Numerology.testMasterNumber(Numerology.java:46)
在java.lang.reflect.Method.invokeNative(本机方法)
在android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
在android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
在android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
在android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
在android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
在android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
在android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1701)
 

`

我的布局: `     

 <的TextView
    机器人:ID =@ + ID / lbInfo
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerHorizo​​ntal =真
    机器人:文字颜色=@色/黄
    机器人:文本=你的出生日期请设置
    机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

<的TextView
    机器人:ID =@ + ID / lblDate
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_marginTop =80dp
    机器人:文字颜色=@色/白
    机器人:文本=你的出生日期(DD-MM-YYYY):
    机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

<的TextView
    机器人:ID =@ + ID / tvDate
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_marginTop =100dp
    机器人:文字颜色=@色/白
    机器人:文本=
    机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?

 <的TextView
    机器人:ID =@ + ID / numberInfo
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_marginTop =130dp
    机器人:文字颜色=@色/黄
    机器人:文本=
    机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

  <的TextView
    机器人:ID =@ + ID /说明
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginTop =160dp
    机器人:文字颜色=@色/黄
    机器人:文本=描述:
    机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>


<的DatePicker
    机器人:ID =@ + ID / D presult
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_below =@ + ID / tvDate
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_marginTop =88dp/>

<滚动型
    机器人:ID =@ + ID / scrollView1
    机器人:layout_marginTop =30dp
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignTop =@ + ID /描述>

<的LinearLayout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>
<的TextView
        机器人:ID =@ + ID / allInformation
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字颜色=@色/白
        机器人:文本=
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>
< / LinearLayout中>
< /滚动型>

< / RelativeLayout的>`
 

我的,我想测试方法: `

 公共无效showTextInfo(INT编号){
        如果(数== 1){
            finInformation.setText(人与振动的数字1,是天生的领导者,谁也受不了过他的权威和希望不惜一切代价,以preserve他们的独立性。自信,一鸣惊人,有效的行动,充满魅力,有一个非常高势能和独立感。他们觉得自我提升的强烈需求,他们是非常有创意的,能够证明自己的主动性,他们有没有问题,善于沟通,能讲漂亮。顶配,实现短期的长期行动。\ñ\ N+
                    性格,个性,渴望创造,创新,优势,大胆的想法,说服力,感召力和出色的领导能力的力量去帮助他们的人和解,哪些指明前进的方向。问鼎通常具有很高的权威性,而且往往模型跟随。+
                    他们有实际意义,坚强的意志,组织能力,但规模较小的执行能力,这些都是动态的性质,特点是自发性,承担风险的意愿。命理雄心勃勃的是那些有能力自我实​​现和组织人才他们是进取,聪明,觉得需要高功率,实现较高的社会地位。+
                    问鼎力求完美,是自我的关键。不容忍别人的批评,他们的弱点,它peremptoriness,自吸,支配,傲慢,危险行为的倾向,该倾向强加自己的意见,急躁。\ñ\ N+
                    故障将导致沮丧,过度紧张和爆发力的感觉。在合作伙伴很容易表达自己的情感,他们觉得非常需要AP preciate,可能过于嫉妒,甚至是占有欲。+
                    对于有一个命理数量很难得到妥协,他们preFER强加自己的观点,看到的方式。);
        }`
 

解决方案

我在我的测试方法前解决这个问题adnotation @UiThreadTest。

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 value into TextView. I got a error

`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)

`

My layout: `

  <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>`

My method which I would like to test: `

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.");
        }`

解决方案

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

这篇关于安卓:只有创建视图层次可以触摸其观点原来的线程 - 的UnitTest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆