【发布时间】:2015-03-16 14:21:54
【问题描述】:
我正在尝试构建一个多客户端单 java 服务器模型。我正在使用线程执行此操作,因此每个客户端使用AsyncTask 有一个线程。因此,我正在使用CreateClient 类在onCreate() 方法中创建一个客户端套接字和PrintWriter 对象,然后每次按下button2,我只需将问题/文本传递给服务器即可那个特定的线程。问题是,我在SendMessage 类中得到了一个NullPointerException。
这是我的客户活动:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
//import statements for client
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
//import statements for client
import android.os.AsyncTask;
public class MainActivity extends Activity implements OnClickListener {
EditText question;
//Client sockets
private Socket client;
private PrintWriter printwriter;
private String toTag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create client as soon as the activity starts
CreateClient clientConnection = new CreateClient();
clientConnection.execute();
question = (EditText)findViewById(R.id.editText1);
Button query = (Button)findViewById(R.id.button2);
query.setOnClickListener(this);
ImageButton listen = (ImageButton)findViewById(R.id.button1);
listen.setOnClickListener(this);
listen.performClick();
}
private class CreateClient extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
client = new Socket("Server IP Address", 4444);
printwriter = new PrintWriter(client.getOutputStream(), true);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
private class CloseClient extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
if(printwriter != null) {
try {
printwriter.write("\n");
printwriter.close();
client.close(); // closing the connection
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
private class SendMessage extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
printwriter.write(toTag); // GETTING NULL POINTER EXCEPTION ON THIS LINE
printwriter.write("\n"); //delimiter
printwriter.flush();
return null;
}
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.button2:
if(validate()) //Validate is a function that validates the text
{
toTag = question.getText().toString();
//Invoke the execute method of AsynTask, which will run the doInBackground method of SendMessage Class
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
else
{
//Toast error msg
}
break;
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
CloseClient closeConnection = new CloseClient();
closeConnection.execute();
}
}
【问题讨论】:
-
printwriter显然为空。 -
怎么样?我在创建活动时创建了一个 PrintWriter 对象。 @Kon
标签: java android eclipse client-server printwriter