這是我的服務器端代碼片段。並且對android編程新手我無法修復由LogCat顯示的錯誤消息。我的應用程序意外崩潰,我正在使用模擬器。請幫我或建議我如何解決這些錯誤。謝謝!線程退出與Android套接字編程中未捕獲的異常
package server.activity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Enumeration;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
public class ServerActivity extends Activity {
private TextView serverStatus;
// default ip
public static String SERVERIP = "";
// designate a port
public static final int SERVERPORT = 1234;
private Handler handler = new Handler();
private ServerSocket serverSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server);
serverStatus = (TextView) findViewById(R.id.server_status);
SERVERIP = getLocalIpAddress();
Thread fst = new Thread(new ServerThread());
fst.start();
}
public class ServerThread implements Runnable {
public void run() {
try {
if (SERVERIP != null) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Listening on IP: " + SERVERIP);
}
});
serverSocket = new ServerSocket(SERVERPORT);
while (true) {
// listen for incoming clients
Socket client = serverSocket.accept();
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Connected.");
}
});
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
Log.d("ServerActivity", line);
handler.post(new Runnable() {
@Override
public void run() {
// do whatever you want to the front end
// this is where you can be creative
}
});
}
break;
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
}
});
e.printStackTrace();
}
}
} else {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Couldn't detect internet connection.");
}
});
}
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Error");
}
});
e.printStackTrace();
}
}
}
// gets the ip address of your phone's network
private String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); }
}
}
} catch (SocketException ex) {
Log.e("ServerActivity", ex.toString());
}
return null;
}
@Override
protected void onStop() {
super.onStop();
try {
// make sure you close the socket upon exiting
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
這是從logcat的
11-29 19:33:36.051: D/AndroidRuntime(571): Shutting down VM
11-29 19:33:36.059: W/dalvikvm(571): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
11-29 19:33:36.070: E/AndroidRuntime(571): Uncaught handler: thread main exiting due to uncaught exception
11-29 19:33:36.122: E/AndroidRuntime(571): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{server.activity/server.activity.ServerActivityActivity}: java.lang.ClassNotFoundException: server.activity.ServerActivityActivity in loader [email protected]
11-29 19:33:36.122: E/AndroidRuntime(571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
11-29 19:33:36.122: E/AndroidRuntime(571): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-29 19:33:36.122: E/AndroidRuntime(571): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-29 19:33:36.122: E/AndroidRuntime(571): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-29 19:33:36.122: E/AndroidRuntime(571): at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 19:33:36.122: E/AndroidRuntime(571): at android.os.Looper.loop(Looper.java:123)
11-29 19:33:36.122: E/AndroidRuntime(571): at android.app.ActivityThread.main(ActivityThread.java:4363)
11-29 19:33:36.122: E/AndroidRuntime(571): at java.lang.reflect.Method.invokeNative(Native Method)
11-29 19:33:36.122: E/AndroidRuntime(571): at java.lang.reflect.Method.invoke(Method.java:521)
11-29 19:33:36.122: E/AndroidRuntime(571): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-29 19:33:36.122: E/AndroidRuntime(571): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-29 19:33:36.122: E/AndroidRuntime(571): at dalvik.system.NativeStart.main(Native Method)
11-29 19:33:36.122: E/AndroidRuntime(571): Caused by: java.lang.ClassNotFoundException: server.activity.ServerActivityActivity in loader [email protected]
11-29 19:33:36.122: E/AndroidRuntime(571): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
11-29 19:33:36.122: E/AndroidRuntime(571): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
11-29 19:33:36.122: E/AndroidRuntime(571): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
11-29 19:33:36.122: E/AndroidRuntime(571): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-29 19:33:36.122: E/AndroidRuntime(571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
11-29 19:33:36.122: E/AndroidRuntime(571): ... 11 more
11-29 19:33:36.230: I/dalvikvm(571): threadid=7: reacting to signal 3
11-29 19:33:36.857: I/dalvikvm(571): Wrote stack trace to '/data/anr/traces.txt'
11-29 19:33:46.360: I/Process(571): Sending signal. PID: 571 SIG: 9
感謝alextsc @Last戰士。你的答案幫我修復錯誤,雖然這是一個愚蠢的錯誤:) – aysh1000