【发布时间】:2014-01-08 17:54:16
【问题描述】:
我正在尝试使用PrintWriter#println(message) 发送String 消息,但是一旦我调用该线程,它就会引发异常。
我不知道这条消息是从哪里来的。我检查了消息是不是null,但不是这样。
import java.io.PrintWriter;
import android.util.Log;
public class Send_Client implements Runnable {
private PrintWriter out;
private String message=null;
public Send_Client(PrintWriter out,String message) {
this.out=out;
this.message=message;
}
@Override
public void run() {
// the message is different from null
// Problem to resolve
out.println(message);
out.flush();
Log.d(MainActivity.TAG, "The message has been sent from Send_Client");
}
}
这里是我调用线程的地方
public class ClientDataService extends IntentService {
private static Socket socket=null;
private static final int SOCKET_TIMEOUT = 5000;
public static final String ACTION_CLIENT="Action_Client";
public static final String EXTRAS_GROUP_OWNER_PORT = "go_port";
public static final String EXTRAS_GROUP_OWNER_ADDRESS = "go_host";
String message=null;
private BufferedReader in=null;
public PrintWriter out=null;
/** Command to the service to receive a message */
static final int MSG_CLIENT = 2;
public ClientDataService(String name) {
super(name);
}
public ClientDataService() {
super("ClientDataService");
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent.getAction().equals(ACTION_CLIENT)) {
int port = intent.getExtras().getInt(EXTRAS_GROUP_OWNER_PORT);
String host= intent.getExtras().getString(EXTRAS_GROUP_OWNER_ADDRESS);
try{
Log.d(MainActivity.TAG, "ask for connection to the server");
socket=new Socket();
socket.bind(null);
socket.connect((new InetSocketAddress(host, port)), SOCKET_TIMEOUT);
Log.d(MainActivity.TAG, "connection socket has been established");
try{
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out= new PrintWriter(socket.getOutputStream());
Thread t1=new Thread(new Receive_Client(in));
t1.start();
}catch(IOException e){
Log.d(MainActivity.TAG, "the client disconnect");
}
}catch (UnknownHostException e) {
Log.d(MainActivity.TAG, "impossible to connect to the host"+socket.getLocalSocketAddress());
}catch (IOException e) {
Log.d(MainActivity.TAG, "No Server Listening the port"+socket.getLocalPort());
}
}
}
public PrintWriter getOutputstream(){
return out;
}
/**
* Handler of incoming messages from UI Thread.
*/
class IncomingHandler extends Handler {
ClientDataService mservice;
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_CLIENT:
mservice=new ClientDataService();
Log.d(MainActivity.TAG,"CDS has received the following message to send to the server"+(String)msg.obj);
Log.d(MainActivity.TAG, "Get the Out value"+mservice.getOutputstream().toString());
Thread t2= new Thread(new Send_Client(mservice.getOutputstream(),(String)msg.obj));
t2.start();
break;
default:
super.handleMessage(msg);
}
}
}
/**
* Target we publish for clients to send messages to IncomingHandler.
*/
final Messenger mMessenger = new Messenger(new IncomingHandler());
/**
* When binding to the service, we return an interface to our messenger
* for sending messages to the service.
*/
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(getApplicationContext(), "binding client", Toast.LENGTH_SHORT).show();
return mMessenger.getBinder();
}
}
【问题讨论】:
-
out也是非空的吗? -
NPE 到底是在哪里抛出的?在
out.println(message)行中? -
当
out是null时,NPE 被抛出该行的唯一可能性。所以请检查是否是这种情况。 -
是的,我刚刚检查过,实际上问题出在了。
-
@Veger ,我刚刚修改了帖子。实际上我想使用在 onhandleIntent 中创建的“out”。但是当我尝试使用这个“out”时,应用程序崩溃了。你知道我该如何解决这个问题吗
标签: android exception android-intent android-service