我想在收到来自客户端的数据时显示一条消息。我启动了一个服务器和一个客户端。客户端从服务器发送和接收确认,但在服务器端 android 上,没有显示任何消息,并且应用程序崩溃了,在这里我粘贴了我从 log cat 获得的内容。还有内部类,当我想显示消息时:
import android.widget.Toast;
class Handler implements Runnable {
private Context myContext;
private static final String TAG = "myLogs";
public Handler(Socket client) {
this.client = client;
}
public Handler(final Context context) {
this.myContext= context;
}
public void run() {
try {
//message received..
Toast.makeText(myContext.getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
Log.d(TAG, "received....");
} catch (IOException e) {
System.out.println("Errore: " + e);
}
}
}
日志猫
04-29 17:21:53.679: D/My log(19533): waiting for connnections
04-29 17:24:13.726: D/My Log(19533): Connected 04-29
17:24:14.859:W/dalvikvm(19533): threadid=9: thread exiting with uncaught exception (group=0x40018578) 04-29 17:24:14.859: E/AndroidRuntime(19533): FATAL EXCEPTION: Thread-10 04-29
17:24:14.859: E/AndroidRuntime(19533): java.lang.NullPointerException 04-29
17:24:14.859:E/AndroidRuntime(19533): at com.example.tcpserver.Handler.run(Handler.java:49) 04-29
17:24:14.859: E/AndroidRuntime(19533): at java.lang.Thread.run(Thread.java:1019)