Check current Brightness android

266 views Asked by At

I can't make working my code.. I want check the current brightness and if is in authomatic my ToggleButton will be checked else not. I explain; This is what i'm trying to do to check the brightness:

public static final int SCREEN_BRIGHTNESS_MODE_AUTOMATIC = 1;
---
---


int curBrightnessValue = android.provider.Settings.System.getInt(getActivity().getContentResolver(), 
                android.provider.Settings.System.SCREEN_BRIGHTNESS,-1);

        //
        if (curBrightnessValue == 1){
            autoBrightToggle.setEnabled(true);
        } else {
            autoBrightToggle.setEnabled(false);
        }

of course then there is the toggle:

autoBrightToggle = (ToggleButton)v.findViewById(R.id.luminosita);
        autoBrightToggle.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (autoBrightToggle.isChecked()) {
                    setAutoBrightness(true);
                } else {
                    setAutoBrightness(false);
                }
            }
        });
---
---
---
void setAutoBrightness(boolean value) {
        if (value) {
            Settings.System.putInt(getActivity().getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
        } else {
            Settings.System.putInt(getActivity().getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
        }
    }

But doesn't works. it crash with this logCat

12-09 22:03:35.960: E/AndroidRuntime(15136): FATAL EXCEPTION: main
12-09 22:03:35.960: E/AndroidRuntime(15136): Process: com.dd.androreboot, PID: 15136
12-09 22:03:35.960: E/AndroidRuntime(15136): java.lang.NullPointerException
12-09 22:03:35.960: E/AndroidRuntime(15136):    at com.dd.androreboot.devicecontrolfragment.onCreateView(devicecontrolfragment.java:78)
12-09 22:03:35.960: E/AndroidRuntime(15136):    at android.app.Fragment.performCreateView(Fragment.java:1700)
12-09 22:03:35.960: E/AndroidRuntime(15136):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
12-09 22:03:35.960: E/AndroidRuntime(15136):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
12-09 22:03:35.960: E/AndroidRuntime(15136):    at android.app.BackStackRecord.run(BackStackRecord.java:684)
12-09 22:03:35.960: E/AndroidRuntime(15136):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
12-09 22:03:35.960: E/AndroidRuntime(15136):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
12-09 22:03:35.960: E/AndroidRuntime(15136):    at android.os.Handler.handleCallback(Handler.java:733)
12-09 22:03:35.960: E/AndroidRuntime(15136):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-09 22:03:35.960: E/AndroidRuntime(15136):    at android.os.Looper.loop(Looper.java:136)
12-09 22:03:35.960: E/AndroidRuntime(15136):    at android.app.ActivityThread.main(ActivityThread.java:5017)
12-09 22:03:35.960: E/AndroidRuntime(15136):    at java.lang.reflect.Method.invokeNative(Native Method)
12-09 22:03:35.960: E/AndroidRuntime(15136):    at java.lang.reflect.Method.invoke(Method.java:515)
12-09 22:03:35.960: E/AndroidRuntime(15136):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-09 22:03:35.960: E/AndroidRuntime(15136):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-09 22:03:35.960: E/AndroidRuntime(15136):    at dalvik.system.NativeStart.main(Native Method)
1

There are 1 answers

2
mDroid On

David please provide more information or verify your onCreateView() in devicecontrolfragment.java for null values, that's what is causing a problem here. (devicecontrolfragment.java: line 78) As a side note, also make sure you have relevant permissions for this action.