2014-02-06 14 views
0

根据wifi状态,我喜欢将其设置为“开”或“关”,以便在我的操作栏(或活动页面中的任意位置)上有开关。
如果手机的wifi开着,我想将开关设置为开,如果它关闭,则关闭。加载活动时根据条件将开关设置为关闭

我尝试将代码放置在onCreate,onStart - 但它导致应用程序崩溃。

switchWifiToggle.setChecked(Wifi.Enabled()); 

我是新的android/java,否则我会分享堆栈跟踪。
关于logcat有吨和吨的信息,所以我不确定在这里共享什么(我是一个.NET人)。

注册到BroadcastReceiver将无济于事,因为我希望切换设置为正确的状态,而应用程序正在加载(或完成加载)。

这里的图像使用交换机作为主交换机 - 我要将此开关的状态,一旦应用程序被加载的地方,类似的onCreate或在onStart或...

enter image description here

谢谢

更新:堆栈跟踪添加

02-06 09:15:39.943  632-1066/? E/LocSvc_IzatApiV02﹕ W/virtual int izat_core::IzatApiV02::injectLocation(GpsExtLocation):665]: error! inject position failed 
02-06 09:15:39.963  632-1066/? E/LocSvc_ApiV02﹕ W/virtual loc_api_adapter_err LocApiV02::injectPosition(double, double, float):492]: error! status = eLOC_CLIENT_FAILURE_INVALID_PARAMETER, inject_pos_ind.status = UNKNOWN 
02-06 09:15:40.063 16882-16882/com.learn.wifitwo E/WiFiMainActivity﹕ +++ ON CREATE +++ 
02-06 09:15:40.113 16882-16882/com.learn.wifitwo E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: com.learn.wifitwo, PID: 16882 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.learn.wifitwo/com.learn.wifitwo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.learn.wifitwo.WiFi.IsEnabled()' on a null object reference 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:136) 
     at android.app.ActivityThread.main(ActivityThread.java:5017) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.learn.wifitwo.WiFi.IsEnabled()' on a null object reference 
     at com.learn.wifitwo.MainActivity.onCreate(MainActivity.java:99) 
     at android.app.Activity.performCreate(Activity.java:5231) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:136) 
     at android.app.ActivityThread.main(ActivityThread.java:5017) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
+0

发表更多代码。 – Blackbelt

+0

...和LogCat错误堆栈跟踪。基本上,红色的东西;) – 2Dee

+0

让我们说,如果我必须在.NET中这样做,我会有 1)在Form类中,订阅FormLoad事件 2),并在该FormLoad事件中,我会检查参数然后设置swtich的属性。 – Musa

回答

0

“布尔com.l earn.wifitwo.WiFi.IsEnabled()'我猜你有空无线零对象。你调用的IsEnabled方法之前,您应该初始化对象:)我不知道什么是对象的名字,但我可以推测它可能是

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     WiFi wifiObject = new WiFi(); 
     switchWifiToggle.setChecked(wifiObject.isEnabled()); 

    } 

或作出之类的具有WiFi功能的方法静态的,那么你可以使用它就像那样

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     switchWifiToggle.setChecked(WiFI.isEnabled()); 

    }