检查当前的亮度android

时间:2013-12-09 21:05:49

标签: java android togglebutton brightness

我无法使用我的代码..我想检查当前的亮度,如果是在authomatic我的ToggleButton将被检查否则。我解释;这就是我要检查亮度的方法:

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);
        }

当然还有切换:

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);
        }
    }

但不起作用。它崩溃了这个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 个答案:

答案 0 :(得分:0)

David请提供更多信息或验证devicecontrolfragment.java中的onCreateView()是否为空值,这就是导致问题的原因。 (devicecontrolfragment.java:第78行) 另外,请确保您拥有此操作的相关权限。