我有一个连接到远程聊天服务器的应用程序,然后用户可以聊天。我有一个表单,用户在其中输入主机名和端口,然后在另一个表单中,用户查看所有消息并键入新消息。
对于网络,似乎我别无选择,只能使用AsyncTask。但是有些事情我不知道该怎么做。我会在调用onCreate()时在UI中启动网络连接,这不是问题。但是,当我必须连接网络和接口时,它变得一团糟。我创建了两个异步任务-一个用于发送,一个用于接收,但是对我来说不起作用。这是我首先启动SenderTask的代码,该代码应该通过调用接收器任务开始通信,如果应该按下按钮,则会发送新消息:
public class Second extends Activity {
private SenderTask snd;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
// ...
final Button sendButton = (Button) findViewById(R.id.button2);
snd = new SenderTask();
snd.doInBackground(editTexts);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// when the button is clicked the next screen is loaded
snd.onProgressUpdate(true);
}
});
} // end of onCreate
}
然后是SenderTask类:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import android.os.AsyncTask;
import android.widget.EditText;
public class SenderTask extends AsyncTask<EditText, Boolean, Void> {
MulticastSocket so;
InetAddress serverAddress;
int port;
EditText eText1;
EditText eText2;
EditText eText3;
EditText messageBoard;
@Override
protected Void doInBackground(EditText... eTexts) {
eText1 = eTexts[0];
eText2 = eTexts[1];
eText3 = eTexts[2];
messageBoard = eTexts[3];
setUp();
return null;
}
private void setUp() {
// convert the host name to InetAddress
try {
//serverAddress = InetAddress.getByName(eText1.getText().toString());
serverAddress = InetAddress.getByName("my server name is here");
} catch (Exception e) {}
//convert the port to an int
//port = Integer.parseInt(eText2.getText().toString());
port = 4456;
// create socket and start communicating
try {
so = new MulticastSocket(port);
so.joinGroup(serverAddress);
} catch (IOException e) {}
// Start listening should be here
ReceiverTask rec = new ReceiverTask();
rec.doInBackground(messageBoard);
rec.onProgressUpdate(so);
}
private void sendMessage() {
// get the text that they contain and add the new messages to the old ones
String message = eText3.getText().toString();
String conversation = messageBoard.getText().toString();
String newConverstion = conversation.concat("\n[You] ").concat(message);
// make the messages text view editable
messageBoard.setFocusable(true);
messageBoard.setText(newConverstion); // add the new message to the text view
messageBoard.setFocusable(false); // make the messages text view not editable
// erase the text on the second text view that has just been sent
eText3.setText("");
// Send message to server
// convert message to bytes array
byte[] data = (message).getBytes();
// create and send a datagram
DatagramPacket packet = new DatagramPacket(data, data.length, serverAddress, port);
try {
so.send(packet);
} catch (IOException e) {}
} // end of sendMessage
protected void onProgressUpdate(boolean... go) {
if (go.equals(new Boolean(true))) {
sendMessage();
}
}
}
这是ReceiverTask类:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.MulticastSocket;
import android.os.AsyncTask;
import android.widget.EditText;
public class ReceiverTask extends AsyncTask<EditText, MulticastSocket, Void> {
MulticastSocket so;
EditText messageBoard;
@Override
protected Void doInBackground(EditText... messBo) {
messageBoard = messBo[0];
return null;
}
@Override
protected void onProgressUpdate(MulticastSocket... socket) {
so = socket[0];
byte[] data = new byte[1024]; // received data container
while (true) {
try {
// create a datagram for receivin
DatagramPacket packet = new DatagramPacket(data, data.length);
// wait for the next message
so.receive(packet);
String message = new String(data, 0, packet.getLength());
// add the new messages to the old ones
String conversation = messageBoard.getText().toString();
String newConverstion = conversation.concat("\n[Remote] ").concat(message);
// make the messages text view editable
messageBoard.setFocusable(true);
messageBoard.setText(newConverstion); // add the new message to the text view
messageBoard.setFocusable(false); // make the messages text view not editable
} catch (IOException ioe) {}
}
}
}
错误是:
01-25 14:26:09.739: E/AndroidRuntime(661): FATAL EXCEPTION: main
01-25 14:26:09.739: E/AndroidRuntime(661): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.regeduseroox.projx/com.regeduseroox.projx.Second}: java.lang.NullPointerException
01-25 14:26:09.739: E/AndroidRuntime(661): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-25 14:26:09.739: E/AndroidRuntime(661): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-25 14:26:09.739: E/AndroidRuntime(661): at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-25 14:26:09.739: E/AndroidRuntime(661): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-25 14:26:09.739: E/AndroidRuntime(661): at android.os.Handler.dispatchMessage(Handler.java:99)
01-25 14:26:09.739: E/AndroidRuntime(661): at android.os.Looper.loop(Looper.java:137)
01-25 14:26:09.739: E/AndroidRuntime(661): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-25 14:26:09.739: E/AndroidRuntime(661): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 14:26:09.739: E/AndroidRuntime(661): at java.lang.reflect.Method.invoke(Method.java:511)
01-25 14:26:09.739: E/AndroidRuntime(661): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-25 14:26:09.739: E/AndroidRuntime(661): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-25 14:26:09.739: E/AndroidRuntime(661): at dalvik.system.NativeStart.main(Native Method)
01-25 14:26:09.739: E/AndroidRuntime(661): Caused by: java.lang.NullPointerException
01-25 14:26:09.739: E/AndroidRuntime(661): at com.regeduseroox.projx.ReceiverTask.onProgressUpdate(ReceiverTask.java:33)
01-25 14:26:09.739: E/AndroidRuntime(661): at com.regeduseroox.projx.SenderTask.setUp(SenderTask.java:53)
01-25 14:26:09.739: E/AndroidRuntime(661): at com.regeduseroox.projx.SenderTask.doInBackground(SenderTask.java:27)
01-25 14:26:09.739: E/AndroidRuntime(661): at com.regeduseroox.projx.Second.onCreate(Second.java:38)
01-25 14:26:09.739: E/AndroidRuntime(661): at android.app.Activity.performCreate(Activity.java:4465)
01-25 14:26:09.739: E/AndroidRuntime(661): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-25 14:26:09.739: E/AndroidRuntime(661): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-25 14:26:09.739: E/AndroidRuntime(661): ... 11 more
ReceiverTask.java:33
是so.receive(packet);
SenderTask.java:53
是rec.onProgressUpdate(so);
SenderTask.java:27
是setUp();
Second.java:38
是snd.doInBackground(editTexts);
我不知道现在出了什么问题,我真的再也没有解决该问题的想法。任何帮助都感激不尽。
最佳答案
更换
snd.doInBackground(editTexts);
与
snd.execute(editTexts);
编辑
您不应直接调用
onProgressUpdate()
或doInBackground()
来启动AsyncTask,而只能调用execute()
。此外,onProgressUpdate()
在UI线程上完成其工作,实际上它仅用于更新ProgressBar
。您应该将所有工作从onProgressUpdate()
移到doInBackground()
,并在onPostExecute()
中返回数据。我的建议是创建一个包含
String
(对话)和MulticastSocket
(so)作为其字段的自定义类。在AsyncTask.execute()
调用中发送该对象,然后从varargs中检索。例如:
public class MyObject {
MulticastSocket so;
EditText messageBoard;
//... Getters and Setters omitted for brevity
}
然后在您的SenderTask中:
ReceiverTask rec = new ReceiverTask();
MyObject obj = new MyObject(so, messageBoard);
rec.execute(obj);
您应该对Second / SenderTask使用相同的想法(将工作从
onProgressUpdate()
中删除)。让我知道是否很清楚,请务必重新阅读docs on the proper usage of AsyncTask.