0

Android クライアントは、Android 2.2 で問題なく動作します。しかし、Android 4.2 では AsyncTask を使用する必要があり、適切に設定する方法がわかりません。ボタンを押すと、クライアントは TCP サーバーに接続する必要があります。以下のコードで試してみましたが、FATAL EXCEPTION: 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();            
            }
        };

これは私が得るエラーです;

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
4

2 に答える 2