Android(java)学习笔记116:PC_Phone通信程序报错

1.首先我写的程序代码如下:

 1 package com.himi.udpsend;  2 

 3 import java.net.DatagramPacket;  4 import java.net.DatagramSocket;  5 import java.net.DatagramSocketImpl;  6 import java.net.InetAddress;  7 import java.net.SocketException;  8 import java.net.UnknownHostException;  9 

10 import android.app.Activity; 11 import android.content.Context; 12 import android.net.wifi.WifiInfo; 13 import android.net.wifi.WifiManager; 14 import android.os.Bundle; 15 import android.util.Log; 16 import android.view.View; 17 import android.view.View.OnClickListener; 18 import android.widget.Button; 19 import android.widget.EditText; 20 

21 

22 public class MainActivity extends Activity implements OnClickListener { 23     private EditText edittext; 24     private Button btnsend; 25     private WifiInfo wifiInfo; 26  @Override 27     protected void onCreate(Bundle savedInstanceState) { 28         super.onCreate(savedInstanceState); 29  setContentView(R.layout.activity_main); 30         

31         edittext = (EditText)findViewById(R.id.edit); 32         btnsend = (Button)findViewById(R.id.btnsend); 33         

34         btnsend.setOnClickListener(this); 35         

36         WifiManager wman = (WifiManager) getSystemService(Context.WIFI_SERVICE); 37         

38         if(! wman.isWifiEnabled()) { 39             wman.setWifiEnabled(true); 40  } 41         Log.e("ip", intToIp(wifiInfo.getIpAddress()));//获得本机ip

42         

43  } 44     

45     

46     public String intToIp(int i) { 47         return (i & 0xff) +"."+

48           ((i>>8) & 0xff )+"."+

49           ((i>>16) & 0xff )+"."+

50           ((i>>24) & 0xff ); 51                 

52  } 53 

54 

55 

56 

57     public void onClick(View v) { 58 

59         try { 60             // 创建发送端socket对象

61             DatagramSocket ds = new DatagramSocket(); 62 

63             // 创建数据并打包

64             String edit_content = edittext.getText().toString(); 65             byte[] bys = edit_content.getBytes(); 66             int length = bys.length; 67 

68             String ip = intToIp(wifiInfo.getIpAddress()); 69             InetAddress hostip = InetAddress.getByName(ip); 70             

71             int port = 10086; 72             DatagramPacket dp = new DatagramPacket(bys, length, hostip, port); 73             

74             //发送数据包

75  ds.send(dp); 76             //关闭套接字,回收资源

77  ds.close(); 78 

79         } catch (Exception e) { 80             // TODO 自动生成的 catch 块

81  e.printStackTrace(); 82  } 83         

84         

85  } 86 

87 

88     

89     

90 }

2.报错如下:

06-26 02:37:29.270: E/Trace(841): error opening trace file: No such file or directory (2) 06-26 02:37:30.400: D/AndroidRuntime(841): Shutting down VM 06-26 02:37:30.400: W/dalvikvm(841): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 06-26 02:37:30.540: E/AndroidRuntime(841): FATAL EXCEPTION: main 06-26 02:37:30.540: E/AndroidRuntime(841): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.himi.udpsend/com.himi.udpsend.MainActivity}: java.lang.NullPointerException 06-26 02:37:30.540: E/AndroidRuntime(841):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 06-26 02:37:30.540: E/AndroidRuntime(841):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 06-26 02:37:30.540: E/AndroidRuntime(841):     at android.app.ActivityThread.access$600(ActivityThread.java:141) 06-26 02:37:30.540: E/AndroidRuntime(841):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 06-26 02:37:30.540: E/AndroidRuntime(841):     at android.os.Handler.dispatchMessage(Handler.java:99) 06-26 02:37:30.540: E/AndroidRuntime(841):     at android.os.Looper.loop(Looper.java:137) 06-26 02:37:30.540: E/AndroidRuntime(841):     at android.app.ActivityThread.main(ActivityThread.java:5041) 06-26 02:37:30.540: E/AndroidRuntime(841): at java.lang.reflect.Method.invokeNative(Native Method) 06-26 02:37:30.540: E/AndroidRuntime(841):     at java.lang.reflect.Method.invoke(Method.java:511) 06-26 02:37:30.540: E/AndroidRuntime(841):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 06-26 02:37:30.540: E/AndroidRuntime(841):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-26 02:37:30.540: E/AndroidRuntime(841): at dalvik.system.NativeStart.main(Native Method) 06-26 02:37:30.540: E/AndroidRuntime(841): Caused by: java.lang.NullPointerException 06-26 02:37:30.540: E/AndroidRuntime(841):     at com.himi.udpsend.MainActivity.onCreate(MainActivity.java:41) 06-26 02:37:30.540: E/AndroidRuntime(841):     at android.app.Activity.performCreate(Activity.java:5104) 06-26 02:37:30.540: E/AndroidRuntime(841):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 06-26 02:37:30.540: E/AndroidRuntime(841):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 06-26 02:37:30.540: E/AndroidRuntime(841):     ... 11 more 06-26 02:37:34.910: I/Process(841): Sending signal. PID: 841 SIG: 9

原因:空指针异常,这时就要查看自己的程序中是否存在空指针。

 

4.解决办法是:

 1 package com.himi.udpsend;  2 

 3 import java.net.DatagramPacket;  4 import java.net.DatagramSocket;  5 import java.net.DatagramSocketImpl;  6 import java.net.InetAddress;  7 import java.net.SocketException;  8 import java.net.UnknownHostException;  9 

10 import android.app.Activity; 11 import android.content.Context; 12 import android.net.wifi.WifiInfo; 13 import android.net.wifi.WifiManager; 14 import android.os.Bundle; 15 import android.util.Log; 16 import android.view.View; 17 import android.view.View.OnClickListener; 18 import android.widget.Button; 19 import android.widget.EditText; 20 

21 

22 public class MainActivity extends Activity implements OnClickListener { 23     private EditText edittext; 24     private Button btnsend; 25     private WifiInfo wifiInfo; 26  @Override 27     protected void onCreate(Bundle savedInstanceState) { 28         super.onCreate(savedInstanceState); 29  setContentView(R.layout.activity_main); 30         

31         edittext = (EditText)findViewById(R.id.edit); 32         btnsend = (Button)findViewById(R.id.btnsend); 33         

34         btnsend.setOnClickListener(this); 35         

36         WifiManager wman = (WifiManager) getSystemService(Context.WIFI_SERVICE); 37         

38         if(! wman.isWifiEnabled()) { 39             wman.setWifiEnabled(true); 40  }//没有正确使用Log() 41         //Log.e("ip", intToIp(wifiInfo.getIpAddress()));//获得本机ip

42         

43  } 44     

45     

46     public String intToIp(int i) { 47         return (i & 0xff) +"."+

48           ((i>>8) & 0xff )+"."+

49           ((i>>16) & 0xff )+"."+

50           ((i>>24) & 0xff ); 51                 

52  } 53 

54 

55 

56 

57     public void onClick(View v) { 58 

59         try { 60             // 创建发送端socket对象

61             DatagramSocket ds = new DatagramSocket(); 62 

63             // 创建数据并打包

64             String edit_content = edittext.getText().toString(); 65             byte[] bys = edit_content.getBytes(); 66             int length = bys.length; 67 

68             String ip = intToIp(wifiInfo.getIpAddress()); 69             InetAddress hostip = InetAddress.getByName(ip); 70             

71             int port = 10086; 72             DatagramPacket dp = new DatagramPacket(bys, length, hostip, port); 73             

74             //发送数据包

75  ds.send(dp); 76             //关闭套接字,回收资源

77  ds.close(); 78 

79         } catch (Exception e) { 80             // TODO 自动生成的 catch 块

81  e.printStackTrace(); 82  } 83         

84         

85  } 86 

87 

88     

89     

90 }

 

你可能感兴趣的:(android)