1

是的,我认为就这一点而言,您应该将我视为一个完整的菜鸟,我已经过头了。

我试图从几个部分教程中拼凑出我对此的理解,每个教程都涉及我想做的不同方面。

总而言之,我想做的就是从 URL 读取文本以显示在文本视图中,我的理解是自 3.0 更新以来我需要一个 AsyncTask 来执行此操作。

任何关于我哪里出错的帮助都会很棒,因为我不明白是什么导致了错误以及如何解决。

我确实知道 doInBackground 的 try 和 catch 中包含的代码在 3.0 更新之前可以自行运行。这是否也需要修改以使用 Async 但我不知道。

我还想,一旦对这个问题进行排序,可能会出现其他问题,如果有人发现一些明显的东西,如果你能指出的话,我将不胜感激。

String Event;
TextView eventText;
TextView titleText;

String HTML;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newevent);

    titleText = (TextView) findViewById(R.id.Title);
    eventText = (TextView) findViewById(R.id.Event);

    new eventupdate().execute();


}

public class eventupdate extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... url) {

        try {

            Thread.sleep(4000);                 

            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpGet httpGet = new HttpGet(
                    "http://masterzangetsu.eu/Apps/rocksoctest"); // URL!
            HttpResponse response = httpClient.execute(httpGet,
                    localContext);
            String result = "";

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(response.getEntity().getContent()));

            String line = null;
            while ((line = reader.readLine()) != null) {
                result += line + "\n";
                HTML = result;
            }

        } catch (InterruptedException e) {

            e.printStackTrace();


        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        eventText.setText("" + HTML);

        // TODO Auto-generated method stub
        return null;
    }


}

LogCat 错误是:

W/dalvikvm(394): threadid=9: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime(394): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(394): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(394):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
E/AndroidRuntime(394):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
E/AndroidRuntime(394):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
E/AndroidRuntime(394):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
E/AndroidRuntime(394):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
E/AndroidRuntime(394):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
E/AndroidRuntime(394):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
E/AndroidRuntime(394):  at java.lang.Thread.run(Thread.java:1019)

E/AndroidRuntime(394): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

E/AndroidRuntime(394):  at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
E/AndroidRuntime(394):  at android.view.ViewRoot.requestLayout(ViewRoot.java:629)
E/AndroidRuntime(394):  at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394):  at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394):  at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394):  at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394):  at android.widget.ScrollView.requestLayout(ScrollView.java:1291)
E/AndroidRuntime(394):  at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394):  at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
E/AndroidRuntime(394):  at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394):  at android.widget.TextView.checkForRelayout(TextView.java:5521)
E/AndroidRuntime(394):  at android.widget.TextView.setText(TextView.java:2724)
E/AndroidRuntime(394):  at android.widget.TextView.setText(TextView.java:2592)
E/AndroidRuntime(394):  at android.widget.TextView.setText(TextView.java:2567)
E/AndroidRuntime(394):  at com.MasterZangetsu.kentrocksoc.nextEvent$eventupdate.doInBackground(nextEvent.java:81)
E/AndroidRuntime(394):  at com.MasterZangetsu.kentrocksoc.nextEvent$eventupdate.doInBackground(nextEvent.java:1)
E/AndroidRuntime(394):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
E/AndroidRuntime(394):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
4

1 回答 1

5

这条线在这里

 eventText.setText("" + HTML);

导致你的问题。您无法更新UIfrom doInBackground()。它必须在其他方法之一中完成或发送回主要Activity方法。在这里你可以做到onPostExecute()

通读 AyncTask 文档

有不同的方法可以实现这一点,但您可以将AsyncTask返回结果更改为onPostExecute()

public class eventupdate extends AsyncTask<String, Void, String> // change last param

然后在中添加onPostExecute()方法和更改return语句doInBackground()

public class eventupdate extends AsyncTask<String, Void, String>

 @Override
 protected void onPostExecute(String result) {
        super.onPostExecute(result);
         eventText.setText("" + result);
}

 @Override
    protected String doInBackground(String... url) {
   // do your work
   return HTML;
于 2013-04-05T13:37:26.440 回答