我正在尝试向https://www.google.com发送请求并获得回复。我使用asyncTask但得到以下异常

时间:2013-12-17 05:43:11

标签: android

12-17 11:01:44.227: E/AndroidRuntime(1768): FATAL EXCEPTION: AsyncTask #1
12-17 11:01:44.227: E/AndroidRuntime(1768): java.lang.RuntimeException: An error occured while executing doInBackground()
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at java.lang.Thread.run(Thread.java:856)
12-17 11:01:44.227: E/AndroidRuntime(1768): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4607)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.view.View.requestLayout(View.java:15129)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.view.View.requestLayout(View.java:15129)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.view.View.requestLayout(View.java:15129)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.view.View.requestLayout(View.java:15129)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.view.View.requestLayout(View.java:15129)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.widget.TextView.checkForRelayout(TextView.java:6303)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.widget.TextView.setText(TextView.java:3547)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.widget.TextView.setText(TextView.java:3405)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.widget.TextView.setText(TextView.java:3380)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at com.example.internet.MainActivity$RequestTask.doInBackground(MainActivity.java:150)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at com.example.internet.MainActivity$RequestTask.doInBackground(MainActivity.java:1)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-17 11:01:44.227: E/AndroidRuntime(1768):     ... 5 more
12-17 11:01:44.857: W/IInputConnectionWrapper(1768): showStatusIcon on inactive InputConnection

2 个答案:

答案 0 :(得分:1)

这是原因。

Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

用户界面无法从doInBackground

更新

您也应该提供代码,特别是相关部分。

以下是可以使用的方法示例

private class SaveProfile extends AsyncTask<String, Void, Boolean>{

@Override
protected Boolean doInBackground(String... params) {
    //---------- so your stuff here.... non ui related

    Log.v("response from saving",response);

    if(response.equals("1")){

        return true;                
    }else{

        return false;
    }
}

protected void onPostExecute(Boolean result) {

     if(result) {

                  //------ UPDATE UI HERE
         Toast.makeText(ProfileCompanyActivity.this, "profile saved", 2500).show();
     }else{
         Toast.makeText(ProfileCompanyActivity.this, "an error occured", 2500).show();
     }
 }
}

答案 1 :(得分:0)

首先,您必须了解Android中的AsynTask概念...

1)你无法在backGround线程中更新或更改你的UI元素,即doInBackGround()

2)使用onPreExcecute()或onPostExcecute()来更改或更新UI元素

确定您正在将UI元素访问到doInBackGround

Check out this Example to Understant how the Methods work

希望这可以帮助......