我正在尝试在我的Android手机上获取我的wlan0界面的本地InetAddress
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import java.net.InetAddress;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
InetAddress ia = Utils.getLocalInetIP4Address("wlan0");
setContentView(R.layout.activity_main);
}
}
这是Utils.java
:
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.InterfaceAddress;
import java.net.SocketException;
import java.util.Enumeration;
public class Utils {
public static InetAddress getLocalInetIP4Address(String nw_if) {
try {
Enumeration<NetworkInterface> interfs = NetworkInterface.getNetworkInterfaces();
for(NetworkInterface interf; interfs.hasMoreElements();) {
interf = interfs.nextElement();
if(interf.getName().equals(nw_if)) {
Enumeration<InetAddress> inetaddrs = interf.getInetAddresses(); //Get all IPs (IPv4 or IPv6) associated to this interface in an InetAddress object
for(InetAddress addr; inetaddrs.hasMoreElements();) {
addr = inetaddrs.nextElement();
if(addr instanceof Inet4Address) {
return addr;
}
}
}
}
} catch(SocketException ex) {
System.out.println(ex.getMessage());
}
return null;
}
}
当我启动应用程序时它没有启动,在屏幕上我收到“应用程序停止运行”的消息(或类似英语中的内容)。
这些是我得到的错误:
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{example.antonio.broadcast_adhoc/example.antonio.broadcast_adhoc.MainActivity}: java.lang.NullPointerException
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:156)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at android.os.Looper.loop(Looper.java:153)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5297)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: Caused by: java.lang.NullPointerException
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at example.antonio.broadcast_adhoc.Utils.getLocalInetIP4Address(Utils.java:29)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at example.antonio.broadcast_adhoc.MainActivity.onCreate(MainActivity.java:12)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5122)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:156)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at android.os.Looper.loop(Looper.java:153)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5297)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
10-18 19:28:09.371 14961-14961/example.antonio.broadcast_adhoc E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
答案 0 :(得分:0)
您已省略AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />