Android:无法以pramically方式连接到WiFi网络

时间:2015-04-15 12:19:33

标签: java android wifi

我尝试使用以下方法连接特定的Wifi:

if (wifiChecking.isWifiEnabled()) {
            Toast.makeText(this, "Wifi is working ...", Toast.LENGTH_SHORT)
                    .show();
        } else {
            String networkSSID = "adhocNet";
            Toast.makeText(this, "Wifi is off ...", Toast.LENGTH_SHORT).show();
            wifiChecking.setWifiEnabled(true);

            WifiConfiguration conf = new WifiConfiguration();
            conf.SSID = "\"\"" + networkSSID + "\"\"";
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            wifiChecking.addNetwork(conf);

            List<WifiConfiguration> list = wifiChecking.getConfiguredNetworks();
            for (WifiConfiguration i : list) {
                if (i.SSID != null
                        && i.SSID.equals("\"\"" + networkSSID + "\"\"")) {
                    wifiChecking.disconnect();
                    wifiChecking.enableNetwork(i.networkId, true);
                    System.out.print("i.networkId " + i.networkId + "\n");
                    wifiChecking.reconnect();
                    break;

                }
            }
        }

然而,它总是在这一行崩溃:

for (WifiConfiguration i : list ){ 

这是LogCat输出:

04-15 13:29:14.010: W/dalvikvm(3476): threadid=1: thread exiting with uncaught exception (group=0x415837c0)
04-15 13:29:14.020: E/AndroidRuntime(3476): FATAL EXCEPTION: main
04-15 13:29:14.020: E/AndroidRuntime(3476): java.lang.IllegalStateException: Could not execute method of the activity
04-15 13:29:14.020: E/AndroidRuntime(3476):     at android.view.View$1.onClick(View.java:3640)
04-15 13:29:14.020: E/AndroidRuntime(3476):     at android.view.View.performClick(View.java:4247)
04-15 13:29:14.020: E/AndroidRuntime(3476):     at android.view.View$PerformClick.run(View.java:17728)
04-15 13:29:14.020: E/AndroidRuntime(3476):     at android.os.Handler.handleCallback(Handler.java:730)
04-15 13:29:14.020: E/AndroidRuntime(3476):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-15 13:29:14.020: E/AndroidRuntime(3476):     at android.os.Looper.loop(Looper.java:137)
04-15 13:29:14.020: E/AndroidRuntime(3476):     at android.app.ActivityThread.main(ActivityThread.java:5289)
04-15 13:29:14.020: E/AndroidRuntime(3476):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 13:29:14.020: E/AndroidRuntime(3476):     at java.lang.reflect.Method.invoke(Method.java:525)
04-15 13:29:14.020: E/AndroidRuntime(3476):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
04-15 13:29:14.020: E/AndroidRuntime(3476):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
04-15 13:29:14.020: E/AndroidRuntime(3476):     at dalvik.system.NativeStart.main(Native Method)
04-15 13:29:14.020: E/AndroidRuntime(3476): Caused by: java.lang.reflect.InvocationTargetException
04-15 13:29:14.020: E/AndroidRuntime(3476):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 13:29:14.020: E/AndroidRuntime(3476):     at java.lang.reflect.Method.invoke(Method.java:525)
04-15 13:29:14.020: E/AndroidRuntime(3476):     at android.view.View$1.onClick(View.java:3635)
04-15 13:29:14.020: E/AndroidRuntime(3476):     ... 11 more
04-15 13:29:14.020: E/AndroidRuntime(3476): Caused by: java.lang.NullPointerException
04-15 13:29:14.020: E/AndroidRuntime(3476):     at com.example.testApp.MainActivity.enableAdohcNetworking(MainActivity.java:52)
04-15 13:29:14.020: E/AndroidRuntime(3476):     ... 14 more

任何想法可能是什么问题?!

请注意,我正在尝试连接到ad-hoc网络并且设备已植根并且

0 个答案:

没有答案