这是我的代码,它自己运行的代码,但我试图用新行将所有数据添加到我的Textview1,所以它以这种格式显示
22:22
13:44
33:33
xx:xx
XX:XX
但应用程序崩溃是因为这个Textview1代码出错了。
private TextView textView1;
的OnCreate
textView1 = (TextView)findViewById(R.id.textView1);
请告诉我为什么我的Textview1崩溃应用程序?
for(int j=0;j<hrefElements.size();j++)
{
//System.out.println("Links: "+links[j]);
System.out.println("Title: "+title[j]);
textView1.setText("SUBH: "+timee[j] +" \n");
System.out.println("SUBH: "+timee[j]);
System.out.println("DUHR: "+timee1[j]);
System.out.println("ASR: "+timee2[j]);
System.out.println("MAGHRIB: "+timee3[j]);
System.out.println("ISHA: "+timee4[j]);
stacktracr error log
Process: testtt.myapplication, PID: 22819
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6915)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1056)
at android.view.View.requestLayout(View.java:17321)
at android.view.View.requestLayout(View.java:17321)
at android.view.View.requestLayout(View.java:17321)
at android.view.View.requestLayout(View.java:17321)
at android.view.View.requestLayout(View.java:17321)
at android.view.View.requestLayout(View.java:17321)
at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
at android.view.View.requestLayout(View.java:17321)
at android.widget.TextView.checkForRelayout(TextView.java:8009)
at android.widget.TextView.setText(TextView.java:4841)
at android.widget.TextView.setText(TextView.java:4673)
at android.widget.TextView.setText(TextView.java:4648)
at testtt.myapplication.MainActivity$JsoupAsyncTask.doInBackground(MainActivity.java:102)
at testtt.myapplication.MainActivity$JsoupAsyncTask.doInBackground(MainActivity.java:64)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
08-14 03:45:04.741 22819-22819/testtt.myapplication E/ActivityThread﹕ Performing pause of activity that is not resumed: {testtt.myapplication/testtt.myapplication.MainActivity}
java.lang.RuntimeException: Performing pause of activity that is not resumed: {testtt.myapplication/testtt.myapplication.MainActivity}
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3215)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3203)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3181)
at android.app.ActivityThread.access$1100(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
thread that created a view hierarchy can touch its views.
您正在从后台线程设置textview的文本。您必须在主(UI)线程上执行此操作。更改您的AsyncTask
只需在doInBackground()
中构建字符串,然后更新onPostExecute()
中的文字视图。