当我开始新线程时,我的应用程序崩溃了

时间:2013-08-31 21:59:28

标签: java android multithreading android-intent datagram

我有一个类,我已经实现了runnable,我在这个类的一个函数中启动了线程,我从main活动中调用了这个函数,我创建了这个类的对象并调用了线程类的方法我调用这个类方法的主要活动代码是:

  broadcast broadcastobject.threadfunc(messages);

我创建线程的类是:

  public class broadcast  {

private DatagramSocket socket;
String str;
private static final int TIMEOUT_MS = 10;
WifiManager mWifi;
EditText et;
DatagramPacket packet;
Button bt;
private static final int SERVERPORT = 11111;
private static final String SERVER_IP = "192.168.1.255";


public void threadfunc(String message){
    str=message;
                new Thread(new ClientThread()).start();
}

/*
 private InetAddress getBroadcastAddress() throws IOException {
        DhcpInfo dhcp = mWifi.getDhcpInfo();
        if (dhcp == null) {
          //Log.d(TAG, "Could not get dhcp info");
          return null;
        }

        int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
        byte[] quads = new byte[4];
        for (int k = 0; k < 4; k++)
          quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
        return InetAddress.getByAddress(quads);
      }

      */

class ClientThread implements Runnable {

    @Override
    public void run() {


        try {
            socket = new DatagramSocket(SERVERPORT);

            socket.setBroadcast(true);
    //              socket.setSoTimeout(TIMEOUT_MS);
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

            InetAddress serverAddr = null;
            try {


                serverAddr = InetAddress.getByName(SERVER_IP);


            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

           packet = new DatagramPacket(str.getBytes(), str.length(),serverAddr,SERVERPORT);

            try {

                    socket.send(packet);

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    } 

    }

}

我的日志猫是:

   08-31 21:55:56.277: D/gralloc_goldfish(1669): Emulator without GPU emulation detected.
   08-31 21:56:02.467: D/AndroidRuntime(1669): Shutting down VM
   08-31 21:56:02.467: W/dalvikvm(1669): threadid=1: thread exiting with uncaught  exception (group=0x409961f8)
   08-31 21:56:02.517: E/AndroidRuntime(1669): FATAL EXCEPTION: main
   08-31 21:56:02.517: E/AndroidRuntime(1669): java.lang.NullPointerException
   08-31 21:56:02.517: E/AndroidRuntime(1669):  at soft.b.peopleassist.Send$1.onClick(Send.java:113)
    08-31 21:56:02.517: E/AndroidRuntime(1669):     at android.view.View.performClick(View.java:3480)
   08-31 21:56:02.517: E/AndroidRuntime(1669):  at android.view.View$PerformClick.run(View.java:13983)
    08-31 21:56:02.517: E/AndroidRuntime(1669):     at android.os.Handler.handleCallback(Handler.java:605)
    08-31 21:56:02.517: E/AndroidRuntime(1669):     at android.os.Handler.dispatchMessage(Handler.java:92)
     08-31 21:56:02.517: E/AndroidRuntime(1669):    at android.os.Looper.loop(Looper.java:137)
       08-31 21:56:02.517: E/AndroidRuntime(1669):  at android.app.ActivityThread.main(ActivityThread.java:4340)
      08-31 21:56:02.517: E/AndroidRuntime(1669):   at java.lang.reflect.Method.invokeNative(Native Method)
      08-31 21:56:02.517: E/AndroidRuntime(1669):   at java.lang.reflect.Method.invoke(Method.java:511)
     08-31 21:56:02.517: E/AndroidRuntime(1669):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
     08-31 21:56:02.517: E/AndroidRuntime(1669):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
   08-31 21:56:02.517: E/AndroidRuntime(1669):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

这仅表示您使用的仿真器未启用GPU仿真。在Android SDK Tools R15中,您可以启用GPU模拟。您需要创建一个新的模拟器虚拟设备,并在硬件属性中将GPU模拟设置为true。

enter image description here