我在执行此代码时收到错误。
连接到wifi时,该功能应显示MAC地址,否则" ---"
private void DisplayWifiStatus() {
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
if (networkInfo.isConnected()) {
textMac.setText(info.getMacAddress());
}else {
textMac.setText("-----");
}
}
logcat的/堆栈跟踪:
03-11 21:32:07.532 3348-3348/com.light87sakurahimegmail.wifiscanner E/dalvikvm﹕ >>>>> Normal User
03-11 21:32:07.532 3348-3348/com.light87sakurahimegmail.wifiscanner E/dalvikvm﹕ >>>>> com.light87sakurahimegmail.wifiscanner [ userId:0 | appId:10337 ]
03-11 21:32:07.782 3348-3348/com.light87sakurahimegmail.wifiscanner E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.light87sakurahimegmail.wifiscanner, PID: 3348
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.light87sakurahimegmail.wifiscanner/com.light87sakurahimegmail.wifiscanner.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
at android.app.ActivityThread.access$800(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.light87sakurahimegmail.wifiscanner.MainActivity.DisplayWifiStatus(MainActivity.java:79)
at com.light87sakurahimegmail.wifiscanner.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5389)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
at android.app.ActivityThread.access$800(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)