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

时间:2021-10-11 20:43:09

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

 package com.himi.udpsend;

 import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.DatagramSocketImpl;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException; import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity implements OnClickListener {
private EditText edittext;
private Button btnsend;
private WifiInfo wifiInfo; //类Wifiinfo,WifiInfo获取各种Wifi各种信息
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); edittext = (EditText)findViewById(R.id.edit);
btnsend = (Button)findViewById(R.id.btnsend); btnsend.setOnClickListener(this); WifiManager wman = (WifiManager) getSystemService(Context.WIFI_SERVICE); if(! wman.isWifiEnabled()) {
wman.setWifiEnabled(true);
}
Log.e("ip", intToIp(wifiInfo.getIpAddress()));//获得本机ip } public String intToIp(int i) {
return (i & 0xff) +"."+
((i>>8) & 0xff )+"."+
((i>>16) & 0xff )+"."+
((i>>24) & 0xff ); } public void onClick(View v) { try {
// 创建发送端socket对象
DatagramSocket ds = new DatagramSocket(); // 创建数据并打包
String edit_content = edittext.getText().toString();
byte[] bys = edit_content.getBytes();
int length = bys.length; String ip = intToIp(wifiInfo.getIpAddress());
InetAddress hostip = InetAddress.getByName(ip); int port = 10086;
DatagramPacket dp = new DatagramPacket(bys, length, hostip, port); //发送数据包
ds.send(dp);
//关闭套接字,回收资源
ds.close(); } catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} } }

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. 解决办法是:

 package com.himi.udpsend;

 import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.DatagramSocketImpl;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException; import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity implements OnClickListener {
private EditText edittext;
private Button btnsend;
private WifiInfo wifiInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); edittext = (EditText)findViewById(R.id.edit);
btnsend = (Button)findViewById(R.id.btnsend); btnsend.setOnClickListener(this); WifiManager wman = (WifiManager) getSystemService(Context.WIFI_SERVICE); if(! wman.isWifiEnabled()) {
wman.setWifiEnabled(true);
}//没有正确使用Log()
//Log.e("ip", intToIp(wifiInfo.getIpAddress()));//获得本机ip } public String intToIp(int i) {
return (i & 0xff) +"."+
((i>>8) & 0xff )+"."+
((i>>16) & 0xff )+"."+
((i>>24) & 0xff ); } public void onClick(View v) { try {
// 创建发送端socket对象
DatagramSocket ds = new DatagramSocket(); // 创建数据并打包
String edit_content = edittext.getText().toString();
byte[] bys = edit_content.getBytes();
int length = bys.length; String ip = intToIp(wifiInfo.getIpAddress());
InetAddress hostip = InetAddress.getByName(ip); int port = 10086;
DatagramPacket dp = new DatagramPacket(bys, length, hostip, port); //发送数据包
ds.send(dp);
//关闭套接字,回收资源
ds.close(); } catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} } }

附加:

DatagramSocket:套接字用来UDP通信

DatagramPacket:用来包装UDP通信数据包

InetAddress:是Java对IP地址的封装