我不能讓我的代碼工作..我想檢查當前的亮度,如果是自動我的ToggleButton
將被檢查否則不會。我解釋;這就是我想要做的檢查亮度:檢查當前亮度android
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)
devicecontrolfragment.java中的第78行是什麼? – JoelFernandes
this:'autoBrightToggle.setEnabled(false);' –
那麼它意味着autoBrightToggle爲null。但顯然你已經知道這一點。你也可能已經檢查過luminosita存在於'v'的層次結構中,其中v可能來自xml佈局。 – njzk2