我正在尝试将多个数据包从 android 服务器发送到客户端,但问题是代码仅在 2 次迭代后停止,然后它以未捕获的异常退出,所以我做错了什么。你能帮我吗?
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainTalker extends Activity
{
private static DatagramSocket socket;
private static DatagramPacket packet;
private static Thread send;
private Button cl1;
private static EditText ed1;
private static String msg = "Message";
private static byte[] data = new byte[15];
//private static byte[] pktcount = new byte[10];
private static String TAG = "Sender UDP";
static int count=0;
static String[] output = new String[100] ;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_talker);
ed1 = (EditText) findViewById(R.id.msg);
cl1 = (Button) findViewById(R.id.send);
try {
socket = new DatagramSocket(11001);
Toast toast = Toast.makeText(getApplicationContext(),"Port Opened",Toast.LENGTH_SHORT);
toast.show();
System.out.println("Port Opened ");
socket.setBroadcast(true);
} catch (Exception e) {
System.out.println("Exception: Port not opened");
e.printStackTrace();
Log.e(TAG, e.getMessage());
}
cl1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
System.out.println("Button Clicked ");
send = new sendthr();
send.start();
}
});
}
private static class sendthr extends Thread {
public void run()
{
System.out.println("Thread Started");
String messg = ed1.getText().toString();
System.out.println(messg);
for (int i=0; i<=messg.length(); i+=10)
{
if(messg.length()>i+10)
output[i] = messg.substring(i,i+10);
else
output[i]=messg.substring(i);
count++;
output[i+1] =count + ": " + output[i];
System.out.println(output[i+1]);
}
output[0] = Integer.toString(count);
System.out.println(count);
if (messg == null)
messg = msg;
int i =0;
while(i !=count)
{
data = output[i].getBytes();
try
{ //Create packet with target host and target port
packet = new DatagramPacket(data,data.length,InetAddress.getByName("192.168.8.17"), 11001);
System.out.println("Packet Created");
} catch (UnknownHostException e) {
System.out.println("Exception: Packet Created");
e.printStackTrace();
Log.e(TAG, e.getMessage());
}
try
{
System.out.println("Sending the packet "+ msg);
socket.send(packet);
System.out.println("Packet sent");
} catch (IOException e) {
System.out.println("Exception: Packet Send");
e.printStackTrace();
Log.e(TAG, e.getMessage());
}
i++;
}
}
} }
*这是我遇到的错误消息:
11-22 15:58:59.381: W/dalvikvm(1223): threadid=11: thread exiting with uncaught exception (group=0x40a13300)
11-22 15:59:00.251: E/AndroidRuntime(1223): FATAL EXCEPTION: Thread-114
11-22 15:59:00.251: E/AndroidRuntime(1223): java.lang.NullPointerException
11-22 15:59:00.251: E/AndroidRuntime(1223): at com.example.a_talker.MainTalker$sendthr.run(MainTalker.java:89)
11-22 15:59:00.701: W/IInputConnectionWrapper(1223): showStatusIcon on inactive InputConnection*