2013-04-23 356 views
0

Android客戶端在Android 2.2上運行時沒有問題。但在Android 4.2中,我需要使用AsyncTask,並且我不知道如何正確設置它。在按鈕上,客戶端應該連接到TCP服務器。我試過它作爲下面的代碼,但我得到致命的異常:AsyncTask#1錯誤。請你看看我的代碼!謝謝。buttonConnectOnClickListener,致命異常:AsyncTask#1

// ----------------------- CONNECTION BUTTON EVENTLISTENER - begin ---------------------------- 
Button.OnClickListener buttonConnectOnClickListener = new Button.OnClickListener(){ 
@Override 
public void onClick(View arg0) { 



class connectTask extends AsyncTask<String, String, AndroidClientActivity> { 
@Override 
protected AndroidClientActivity doInBackground(String... message) { 





    if(!connected){//if not connected yet 
    outputText("connecting to Server"); 
    try {//try to create a socket and outputstream 
    socket = new Socket("192.168.1.112",5353);//create a socket 
    dataOutputStream = new DataOutputStream(socket.getOutputStream());//and stream 
    outputText("successfully connected");//output the connection status 
    changeConnectionStatus(true);//change the connection status 
    } catch (UnknownHostException e) {//catch and 
    outputText(e.getMessage());//display errors 
    changeConnectionStatus(false); 
    } catch (IOException e) {//catch and 
    outputText(e.getMessage());//display errors 
    changeConnectionStatus(false); 
    } 
    }else{ 
    outputText("disconnecting from Server..."); 
    try {//try to close the socket 
    socket.close(); 
    outputText("successfully disconnected"); 
    changeConnectionStatus(false);//change the connection status 
    } catch (UnknownHostException e) {//catch and 
    outputText(e.getMessage());//display errors 
    } catch (IOException e) {//catch and 
    outputText(e.getMessage());//display errors 
    } 
    } 




    return null; 
    } 

    @Override 
    protected void onPostExecute(AndroidClientActivity jh) { 



    }} 
       new connectTask().execute();    
      } 
     }; 

這是錯誤我geting;

04-23 23:27:30.445: E/AndroidRuntime(12005): FATAL EXCEPTION: AsyncTask #1 
04-23 23:27:30.445: E/AndroidRuntime(12005): java.lang.RuntimeException: An error occured while executing doInBackground() 
04-23 23:27:30.445: E/AndroidRuntime(12005): at android.os.AsyncTask$3.done(AsyncTask.java:278) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at java.lang.Thread.run(Thread.java:856) 
04-23 23:27:30.445: E/AndroidRuntime(12005): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
04-23 23:27:30.445: E/AndroidRuntime(12005): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:709) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at android.view.View.requestLayout(View.java:12863) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at android.view.View.requestLayout(View.java:12863) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at android.view.View.requestLayout(View.java:12863) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at android.view.View.requestLayout(View.java:12863) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:273) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at android.view.View.requestLayout(View.java:12863) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at android.widget.TextView.checkForRelayout(TextView.java:6985) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at android.widget.TextView.setText(TextView.java:3333) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at android.widget.TextView.setText(TextView.java:3189) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at android.widget.TextView.append(TextView.java:2849) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at android.widget.TextView.append(TextView.java:2839) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at com.rovercontrollerandroid.AndroidClientActivity.outputText(AndroidClientActivity.java:249) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at com.rovercontrollerandroid.AndroidClientActivity$3$1connectTask.doInBackground(AndroidClientActivity.java:192) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at com.rovercontrollerandroid.AndroidClientActivity$3$1connectTask.doInBackground(AndroidClientActivity.java:1) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at android.os.AsyncTask$2.call(AsyncTask.java:264) 
04-23 23:27:30.445: E/AndroidRuntime(12005): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
04-23 23:27:30.445: E/AndroidRuntime(12005): ... 5 more 
+1

請格式化你的代碼,以便它是可讀的時你發佈在StackOverflow上。 – Squonk 2013-04-23 22:42:00

回答

1

不要試圖從AsyncTaskdoInBackground(...)訪問任何UI組件。

錯誤發生在logcat 「只有創建視圖層次結構的原始線程可以觸及其視圖。」

doInBackground(...)方法運行在不同的線程上。如果要顯示進度,請將代碼移至onProgressUpdate(...),並使用doInBackground(...)中的publishProgress(...)

doInBackground(...)完成後需要訪問UI的任何其他代碼應該進入onPostExecute(...)

0

從您的logcat,它看起來像outputText()是一個函數,它不會UI操作。看起來你想顯示進度。使用publishProgress()更新AsyncTaskonProgressUpdate()方法。你不能做UI操作中doInBackground()

一個簡短的例子是

class connectTask extends AsyncTask<String, String, AndroidClientActivity> { 
    ProgressDialog progress = new ProgressDialog(MainActivity.this); //create progress dialog 

@Override 
protected AndroidClientActivity doInBackground(String... message) { 
    publishProgress("message to send"); // put this wherever you are trying to show a message while attempting to download 

然後實現

@Override 
protected void onProgressUpdate(String... values) { 
    progress.setTitle("Trying"); 
    progress.setMessage(values[0]); // this will be the value set in `publishProgress() of doINBackground() 
    progress.show(); 
} 

然後調用progress.dismiss()onPostExecute()

相關問題