クライアント/サーバーを介して簡単なチャットを行っています。サーバーにメッセージを送信すると、ListViewバブルが完全に表示されます。しかし、サーバーから送信し、クライアントがそれを受信すると、addItems()を実行しようとし、シャットダウンします。私はすべてを試しました。UIをAsynctaskから変更できないためですが、Android / Javaを初めて使用するため、何をすればよいかわかりません。PLEASEEEEを助けてください!!!!!
***MY LOG CAT** 01-08 18:32:41.023: E/AndroidRuntime(1242): FATAL EXCEPTION: AsyncTask #1
01-08 18:32:41.023: E/AndroidRuntime(1242): java.lang.RuntimeException: An error occured while executing doInBackground()
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-08 18:32:41.023: E/AndroidRuntime(1242): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-08 18:32:41.023: E/AndroidRuntime(1242): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-08 18:32:41.023: E/AndroidRuntime(1242): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-08 18:32:41.023: E/AndroidRuntime(1242): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-08 18:32:41.023: E/AndroidRuntime(1242): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-08 18:32:41.023: E/AndroidRuntime(1242): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-08 18:32:41.023: E/AndroidRuntime(1242): at java.lang.Thread.run(Thread.java:856)
01-08 18:32:41.023: E/AndroidRuntime(1242): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609)
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835)
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.view.View.requestLayout(View.java:15129)
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.view.View.requestLayout(View.java:15129)
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.view.View.requestLayout(View.java:15129)
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.view.View.requestLayout(View.java:15129)
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292)
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.view.View.requestLayout(View.java:15129)
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.widget.AbsListView.requestLayout(AbsListView.java:1932)
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:813)
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:6051)
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:286)
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.widget.ArrayAdapter.add(ArrayAdapter.java:182)
01-08 18:32:41.023: E/AndroidRuntime(1242): at com.i911.emergency.response.DiscussArrayAdapter.add(DiscussArrayAdapter.java:26)
01-08 18:32:41.023: E/AndroidRuntime(1242): at com.i911.emergency.response.HelloBubblesActivity.addItems(HelloBubblesActivity.java:148)
01-08 18:32:41.023: E/AndroidRuntime(1242): at com.i911.emergency.response.HelloBubblesActivity.access$0(HelloBubblesActivity.java:147)
01-08 18:32:41.023: E/AndroidRuntime(1242): at com.i911.emergency.response.HelloBubblesActivity$TcpClientTask.doInBackground(HelloBubblesActivity.java:116)
01-08 18:32:41.023: E/AndroidRuntime(1242): at com.i911.emergency.response.HelloBubblesActivity$TcpClientTask.doInBackground(HelloBubblesActivity.java:1)
01-08 18:32:41.023: E/AndroidRuntime(1242): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-08 18:32:41.023: E/AndroidRuntime(1242): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-08 18:32:41.023: E/AndroidRuntime(1242): ... 5 more
これが私のコードです:
package com.ChatWithMe;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import com.ChatWithMe.OneComment;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class HelloBubblesActivity extends Activity {
private com.ChatWithMe.DiscussArrayAdapter adapter;
private ListView lv;
private EditText editText1;
public Socket s;
public BufferedWriter out;
public BufferedReader in;
public TextView i911Log;
public EditText DataToSend;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_discuss);
lv = (ListView) findViewById(R.id.listView1);
adapter = new DiscussArrayAdapter(getApplicationContext(), R.layout.listitem_discuss);
lv.setAdapter(adapter);
new TcpClientTask().execute();
editText1 = (EditText) findViewById(R.id.editText1);
editText1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
adapter.add(new OneComment(false, "User: " + editText1.getText().toString()));
adapter.add(new OneComment(true, "User: " + editText1.getText().toString()));
try {
out.write("TXT:" + editText1.getText().toString());
out.flush();
} catch (UnknownHostException e) {
e.printStackTrace();
adapter.add(new OneComment(false, "Error Sending: " + editText1.getText().toString()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
adapter.add(new OneComment(false, "Error Sending: " + editText1.getText().toString()));
Log.i("TcpClient", "Button Clicked, but IOException Occured");
}
editText1.setText("");
return true;
}
return false;
}
});
}
class TcpClientTask extends AsyncTask<Void, Void, Void> {
private static final int TCP_SERVER_PORT = 1234;
private boolean error = false;
Boolean SocketStarted = false;
private BufferedReader in;
protected Void doInBackground(Void... arg0) {
try {
Socket s = new Socket("10.0.2.2", TCP_SERVER_PORT);
//10.0.2.2
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//send output msg
String outMsg = "VER:Android,ID:1038263,SND:1,VDO:1,GPS:1,";
out.write(outMsg);
out.flush();
String outMsg2 = com.ChatWithMe.MainActivity.GPSTracker.toString();
out.write(outMsg2);
out.flush();
Log.i("TcpClient", "sent: " + outMsg);
String WhatsIN = in.readLine();
while(WhatsIN!=null){
String inMsg = in.readLine() + System.getProperty("line.separator");
Log.i("TcpClient", "received: " + inMsg);
String inMsg3 = inMsg.substring(1,inMsg.length() - 1);
Log.i("Info", "info: " + inMsg3);
if (inMsg3.equals("GPSX")) {
String GPSTracking = com.ChatWithMe.MainActivity.GPSTracker.toString();
Log.i("GPSX", "Sent:" + GPSTracking.toString());
out.write(GPSTracking);
out.flush();
}
// Check Commands
String CmdCheck = inMsg3.substring(0,4);
if (CmdCheck.equals("TXT:")) {
String SpitItOut = inMsg3.substring(4, inMsg3.length() - 1);
Log.i("SpitItOut", "Msg:" + SpitItOut.toString());
//ERROR IS HERE
addItems();
//ERROR IS HERE
}
}
} catch (UnknownHostException e) {
error = true;
e.printStackTrace();
} catch (IOException e) {
error = true;
e.printStackTrace();
}
return null;
}
protected void onPostExecute() {
if(error) {
// Something bad happened
}
else {
// Success
}
}
}
private void addItems() {
adapter.add(new OneComment(true, "Hello bubbles!"));
}
}