2014-04-01 54 views
0

运行应用程序时,我得到的错误log.e消息“客户端发送错误”和获取错误NullPointerExeption,同时将数据发送到服务器

04-01 16:46:58.001: E/ClientActivity(7741): Client Send Error: 
04-01 16:46:58.001: W/System.err(7741): java.lang.NullPointerException 
04-01 16:46:58.002: W/System.err(7741):  atedu.dongthang.controller.AppDelegate$ClientThread.sendMessage(AppDelegate.java:90) 
04-01 16:46:58.002: W/System.err(7741):  at edu.dongthang.controller.AppDelegate.sendMessage(AppDelegate.java:33) 
04-01 16:46:58.002: W/System.err(7741):  at edu.dongthang.controller.Controller.sendToAppDel(Controller.java:101) 
04-01 16:46:58.002: W/System.err(7741):  at edu.dongthang.controller.Controller.onStart(Controller.java:64) 
04-01 16:46:58.002: W/System.err(7741):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1136) 
04-01 16:46:58.002: W/System.err(7741):  at android.app.Activity.performStart(Activity.java:4489) 
04-01 16:46:58.003: W/System.err(7741):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2050) 
04-01 16:46:58.003: W/System.err(7741):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104) 
04-01 16:46:58.003: W/System.err(7741):  at android.app.ActivityThread.access$600(ActivityThread.java:134) 
04-01 16:46:58.003: W/System.err(7741):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247) 
04-01 16:46:58.003: W/System.err(7741):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-01 16:46:58.004: W/System.err(7741):  at android.os.Looper.loop(Looper.java:154) 
04-01 16:46:58.004: W/System.err(7741):  at android.app.ActivityThread.main(ActivityThread.java:4624) 
04-01 16:46:58.004: W/System.err(7741):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-01 16:46:58.004: W/System.err(7741):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-01 16:46:58.004: W/System.err(7741):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809) 
04-01 16:46:58.005: W/System.err(7741):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576) 
04-01 16:46:58.005: W/System.err(7741):  at dalvik.system.NativeStart.main(Native Method) 

我认为这是产生错误的代码

package edu.dongthang.controller; 
public class AppDelegate extends Application { 

public ClientThread client; 
public int mouse_sensitivity = 1; 
public boolean connected = false; 
public boolean network_reachable = true; 

public void onCreate(){ 
    super.onCreate(); 
} 

public void createClientThread(String ipAddress, int port){ 
    client = new ClientThread(ipAddress, port); 

    Thread cThread = new Thread(client); 
    cThread.start(); 
} 

public void sendMessage(String message){ 
    try { 
     client.sendMessage(message); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

public void stopServer(){ 
    if(connected){ 
     client.closeSocket(); 
    } 
} 

// ClientThread Class implementation 
public class ClientThread implements Runnable { 

    public InetAddress serverAddr; 
    public int serverPort; 
    private DatagramSocket socket; 
    byte[] buf = new byte[1000]; 

    public ClientThread(String ip, int port){ 
     try{ 
      serverAddr = InetAddress.getByName(ip); 
     } 
     catch (Exception e){ 
      Log.e("ClientActivity", "C: Error", e); 
     } 
     serverPort = port; 
    } 

    //Opens the socket and output buffer to the remote server 
    public void run(){ 
     try { 
      socket = new DatagramSocket(); 
      if(socket != null){ 
       socket.setSoTimeout(3000); 
       connected = testConnection(); 
       if(connected) 
        surveyConnection(); 
      } 

     } 
     catch (Exception e) { 
      Log.e("ClientActivity", "Client Connection Error", e); 
     } 
    } 

    public void sendMessage(String message){ 
     try { 
      buf = message.getBytes(); 
      DatagramPacket out = new DatagramPacket(buf, buf.length, serverAddr, serverPort); 
      socket.send(out); 
      network_reachable = true; 
     } 
     catch (Exception e){ 
      Log.e("ClientActivity", "Client Send Error:"); 
      if(e.getMessage().equals("Network unreachable")){ 
       Log.e("ClientActivity", "Netork UNREACHABLE!!!!:"); 
       network_reachable = false; 
      } 
      closeSocketNoMessge(); 
     } 

    } 

    public void closeSocketNoMessge(){ 
     socket.close(); 
     connected = false; 
    } 

    public void closeSocket(){ 
     sendMessage(new String("Close")); 
     socket.close(); 
     connected = false; 
    } 

    private boolean testConnection(){ 
      try { 
       Log.d("Testing", "Sending"); 
       //doi nguoc lai 
       if(!connected)buf = new String("Connectivity").getBytes(); 
       else buf = new String("connected").getBytes(); 

       DatagramPacket out = new DatagramPacket(buf, buf.length, serverAddr, serverPort); 
       socket.send(out); 
       Log.d("Testing", "Sent"); 
       } 
      catch(Exception e){return false;} 

      try{ 
       Log.d("Testing", "Receiving"); 
       DatagramPacket in = new DatagramPacket(buf, buf.length); 
       socket.receive(in); 
       Log.d("Testing", "Received"); 
       return true; 
      } 
      catch(Exception e){return false;} 
    } 

    private void surveyConnection(){ 
     int count = 0; 
     while(connected){ 
      try{Thread.sleep(1000);} 
      catch(Exception e){} 

      if(!testConnection()) 
       count++; 
      else 
       count = 0; 

      if(count == 5){ 
       closeSocket(); 
       return; 
      } 
     } 
    } 

} 
} 

还有一个问题。 uhmmmm同时运行的应用程序,我在logcat的窗口消息看到的

04-01 16:49:28.539: V/Resources(8088): Preloading resource #10805c2(res/drawable-hdpi/textfield_search_right_default_holo_light.9.png) 
04-01 16:49:28.539: V/Resources(8088): Preloading resource #108057e(res/drawable/tab_indicator_holo.xml) 
04-01 16:49:28.548: V/Resources(8088): Preloading resource #108058f(res/drawable-hdpi/tab_unselected_holo.9.png) 
04-01 16:49:28.548: V/Resources(8088): Preloading resource #108058a(res/drawable-hdpi/tab_selected_holo.9.png) 
04-01 16:49:28.548: V/Resources(8088): Preloading resource #108058e(res/drawable-hdpi/tab_unselected_focused_holo.9.png) 
04-01 16:49:28.548: V/Resources(8088): Preloading resource #1080589(res/drawable-hdpi/tab_selected_focused_holo.9.png) 
04-01 16:49:28.549: V/Resources(8088): Preloading resource #1080590(res/drawable-hdpi/tab_unselected_pressed_holo.9.png) 
04-01 16:49:28.549: V/Resources(8088): Preloading resource #108058b(res/drawable-hdpi/tab_selected_pressed_holo.9.png) 
04-01 16:49:28.549: V/Resources(8088): Preloading resource #1080470(res/drawable/quickcontact_badge_overlay_dark.xml) 
04-01 16:49:28.555: V/Resources(8088): Preloading resource #1080474(res/drawable-hdpi/quickcontact_badge_overlay_normal_dark.9.png) 
04-01 16:49:28.555: V/Resources(8088): Preloading resource #1080476(res/drawable-hdpi/quickcontact_badge_overlay_pressed_dark.9.png) 
04-01 16:49:28.555: V/Resources(8088): Preloading resource #1080473(res/drawable/quickcontact_badge_overlay_light.xml) 
04-01 16:49:28.560: V/Resources(8088): Preloading resource #1080475(res/drawable-hdpi/quickcontact_badge_overlay_normal_light.9.png) 
04-01 16:49:28.560: V/Resources(8088): Preloading resource #1080477(res/drawable-hdpi/quickcontact_badge_overlay_pressed_light.9.png) 
在没有这种照片我forder绘制,华电国际

。所以,告诉我,这里有一个问题!

所以,请帮助我,告诉我我错在哪里 非常感谢!

+1

我们需要更多的帮助。你确定这是产生错误的片段吗?如果是的话,哪一行?这种方法用于什么? – sp00m

+0

添加空检查。例如,如果(buff!= null) – Jay

+0

我试过了,但没有效果:( – user2028608

回答

0

您是否检查过消息是否为空?日志似乎指向这个变量。

相关问题