简体   繁体   English

加载活动时,根据情况将开关设置为关闭

[英]Switch set to on off depending on condition while loading an activity

I have a switch on my actionbar (or anywhere in the activity page) which I like to set to either On or Off depending on the wifi status. 我在操作栏上(或活动页面上的任何地方)都有一个开关,我想根据wifi状态将其设置为“开”或“关”。
If phone's wifi is on, I'd like to have the switch set to On and if it's off then to Off. 如果手机的wifi处于开启状态,我希望将开关设置为On(开),如果该开关处于关闭状态,则设置为Off(关)。

I tried placing the code below to onCreate, onStart - but the it causes the app to crash. 我尝试将下面的代码放置到onCreate,onStart上-但这会导致应用崩溃。

switchWifiToggle.setChecked(Wifi.Enabled());

I am new to android/java otherwise I'd share the stack trace. 我是android / java的新手,否则我将共享堆栈跟踪。
There are ton's and ton's of information on the logcat, so I'm not sure what exactly to share here (I am a .NET guy). logcat上有无数的信息,所以我不确定在这里到底要分享什么(我是.NET专家)。

Registering to BroadcastReceiver will not help, as I want the switch to set to the right state while the app is loading (or done loading). 注册到BroadcastReceiver将无济于事,因为我希望在应用加载(或完成加载)时将开关设置为正确的状态。

The image here uses the switch as a Master Switch - I want to set the state of this switch once the app is loaded somewhere, similar to onCreate or onStart or... 此处的图像将开关用作主开关-我希望在应用加载到某个位置(类似于onCreate或onStart或...)后设置此开关的状态。

在此处输入图片说明

Thanks 谢谢

Update: Stack trace added 更新:添加了堆栈跟踪

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)

'boolean com.learn.wifitwo.WiFi.IsEnabled()' I am guessing that you have null WiFi null object. '布尔com.learn.wifitwo.WiFi.IsEnabled()'我猜你有null WiFi null对象。 Before you invoke IsEnabled method you should initialize your Object :) I dont know what is the name of object but I can presume it might be that 在调用IsEnabled方法之前,您应该初始化Object :)我不知道对象的名称是什么,但我可以假定它可能是

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

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

     } 

or made Enabled method in class WiFi a static one then you can use it like that 或将WiFi类中的Enabled方法设置为静态方法,则可以像这样使用它

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

        switchWifiToggle.setChecked(WiFI.isEnabled());

     } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM