我有 FATAL EXCEPTION: MAIN 在运行这个基于套接字的应用程序的客户端部分时:
http://www.edumobile.org/android/android-development/socket-programming/
我正在使用 2 个 eclipse 实例(带有 2 个工作区)和 2 个模拟器
这是相关的logcat:
07-22 13:45:59.039: E/AndroidRuntime(2157): FATAL EXCEPTION: main
07-22 13:45:59.039: E/AndroidRuntime(2157): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.clientsend/com.example.clientsend.ClientSend}: android.os.NetworkOnMainThreadException
07-22 13:45:59.039: E/AndroidRuntime(2157): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-22 13:45:59.039: E/AndroidRuntime(2157): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-22 13:45:59.039: E/AndroidRuntime(2157): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-22 13:45:59.039: E/AndroidRuntime(2157): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-22 13:45:59.039: E/AndroidRuntime(2157): at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 13:45:59.039: E/AndroidRuntime(2157): at android.os.Looper.loop(Looper.java:137)
07-22 13:45:59.039: E/AndroidRuntime(2157): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-22 13:45:59.039: E/AndroidRuntime(2157): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 13:45:59.039: E/AndroidRuntime(2157): at java.lang.reflect.Method.invoke(Method.java:511)
07-22 13:45:59.039: E/AndroidRuntime(2157): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-22 13:45:59.039: E/AndroidRuntime(2157): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-22 13:45:59.039: E/AndroidRuntime(2157): at dalvik.system.NativeStart.main(Native Method)
07-22 13:45:59.039: E/AndroidRuntime(2157): Caused by: android.os.NetworkOnMainThreadException
07-22 13:45:59.039: E/AndroidRuntime(2157): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-22 13:45:59.039: E/AndroidRuntime(2157): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
07-22 13:45:59.039: E/AndroidRuntime(2157): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
07-22 13:45:59.039: E/AndroidRuntime(2157): at libcore.io.IoBridge.connect(IoBridge.java:112)
07-22 13:45:59.039: E/AndroidRuntime(2157): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-22 13:45:59.039: E/AndroidRuntime(2157): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
07-22 13:45:59.039: E/AndroidRuntime(2157): at java.net.Socket.startupSocket(Socket.java:566)
07-22 13:45:59.039: E/AndroidRuntime(2157): at java.net.Socket.<init>(Socket.java:225)
07-22 13:45:59.039: E/AndroidRuntime(2157): at com.example.clientsend.ClientSend.onCreate(ClientSend.java:38)
07-22 13:45:59.039: E/AndroidRuntime(2157): at android.app.Activity.performCreate(Activity.java:5104)
07-22 13:45:59.039: E/AndroidRuntime(2157): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-22 13:45:59.039: E/AndroidRuntime(2157): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-22 13:45:59.039: E/AndroidRuntime(2157): ... 11 more
07-22 13:46:02.770: I/jdwp(2157): Ignoring second debugger -- accepting and dropping
07-22 13:46:03.380: I/Process(2157): Sending signal. PID: 2157 SIG: 9
any clues?
在 AsyncTask 尝试后在此处附加代码:
package com.example.clientsend;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.util.Log;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.os.AsyncTask;
public class ClientSend extends Activity {
private Button invia;
private TextView txt;
private Socket socketC;
private String serverIP = "10.10.45.90";
private static final int PORTASERVER_REDIRETTA = 5000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_send);
invia = (Button) findViewById(R.id.button1);
txt = (TextView) findViewById(R.id.textView1);
try{
InetAddress ipAddress = InetAddress.getByName(serverIP);
socketC = new Socket(ipAddress, PORTASERVER_REDIRETTA);
}catch(UnknownHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
new ActionButton().execute();
}
private class ActionButton extends AsyncTask{
@Override
protected Object doInBackground(Object...objects){
invia.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try{
// prendere il testo dal componente "Editext" e metterlo in una stringa
EditText edit = (EditText) findViewById(R.id.editText1);
String str = edit.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socketC.getOutputStream())), true);
out.println(str);
Log.d("Client", "il Client ha inviato il messaggio");
}catch(UnknownHostException e1){
txt.setText("Error: Host sconosciuto");
e1.printStackTrace();
}catch(IOException e1){
txt.setText("Error: IO");
e1.printStackTrace();
}catch(Exception e1){
txt.setText("Error");
e1.printStackTrace();
}
}
});
return null;
}
}
}