我正在为小应用程序设计UnitTest。这个应用程序说明您使用的是什么命理数字出生日期。我有一个问题,我该调用检查数字并将值放入TextView的方法。我有一个错误
`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.");
}`
实现如下:
/**
* @return The activity under test.
*/
public T getActivity() {
if (mActivity == null) {
Log.w(TAG, "Activity wasn't created yet");
}
return mActivity;
}
由于当getActivity()
返回活动实例时,这意味着它必须创建活动。创建活动时,它必须创建活动的视图。活动视图只能通过UI线程创建。
@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
。
将您将值设置为text view的代码放在新线程中;
Handler h=new Handler();
h.post(new Runnable() {
public void run() {
textView.setText(value);
}
});
看起来lile你的文本视图是finInformation在run()中使用;
在我的测试方法之前,我用adnotation@UiThreadTest解决了这个问题。
我正在使用MultipartEntityBuilder将图像发送到php服务器,我将图片和视频发送到设备摄像头的目录中,我需要将文件保存在新的ArrayList上并将其发送到php服务器。 总是报告错误: 我的代码 更新
我在Android中构建了一个简单的音乐播放器。每首歌的视图都包含一个SeekBar,实现如下: 这很好用。现在我想要一个计时器来计算歌曲进度的秒/分钟。因此,我在布局中放置了一个文本视图,在onCreate()中使用findViewById()获取它,并在。设置进度(pos): 但最后一行给了我一个例外: Android看法ViewRoot$CalledFromErrorThreadExcept
这是我所有的代码。
我有一个错误,只有创建视图层次结构的原始线程才能接触它的视图。但我不知道如何在多线程中处理ui,比如列表视图,我也不知道该放在哪里。
我读了其他帖子,但没有解决我的问题。我是Android的新手。我必须连接到MSSQL,但我得到了原始线程错误。我如何解决这个问题?谢谢 这是我的代码: 在KuyrugaEkle函数中,我动态更改布局,并且我在该函数中收到错误。 谢谢
得到一个错误:android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程可以触摸它的视图。 我真的不知道为什么。 谢谢帮忙。