我正在建立我的学院关于省电的项目。 uptill现在一切正常,但当我尝试通过onClick方法使用Button设置亮度时出现问题 这是代码:
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(position){
case 0:
sysBackLightValue=(int)(backLight*150);
android.provider.Settings.System.putInt(cr, android.provider.Settings.System.SCREEN_BRIGHTNESS, sysBackLightValue);
Toast.makeText(context, "precise brightness Mode", Toast.LENGTH_SHORT).show();
setActions();
break;
}
}
});;
return btn;
}
public void setActions() {
WifiManager wm=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
if(wm.isWifiEnabled()==true){
Toast.makeText(context, "working", Toast.LENGTH_SHORT).show();
wm.setWifiEnabled(false);
count++;
}else if(wm.isWifiEnabled()==false){
Toast.makeText(context, "wifi already Off", Toast.LENGTH_SHORT).show();
}
BluetoothAdapter ba=BluetoothAdapter.getDefaultAdapter();
if(ba.isEnabled()==true){
ba.disable();
count=count++;
}else if(ba.isEnabled()==false){
Toast.makeText(context, "Bluetooth already off", Toast.LENGTH_SHORT).show();
}
Toast.makeText(context,"Extreme Mode enabled",Toast.LENGTH_LONG).show();
}
然后我在亮度代码上得到Null指针异常。
我错过了什么吗? 这个Button不在Activity中,但在BaseAdapter类中,如果它们存在,则生成n个按钮。我得到的错误是:
01-25 18:43:10.599: W/dalvikvm(1288): threadid=1: thread exiting with uncaught exception (group=0x418d7700)
01-25 18:43:10.599: E/AndroidRuntime(1288): FATAL EXCEPTION: main
01-25 18:43:10.599: E/AndroidRuntime(1288): java.lang.NullPointerException
01-25 18:43:10.599: E/AndroidRuntime(1288): at android.provider.Settings$NameValueCache.putStringForUser(Settings.java:860)
01-25 18:43:10.599: E/AndroidRuntime(1288): at android.provider.Settings$System.putStringForUser(Settings.java:1189)
01-25 18:43:10.599: E/AndroidRuntime(1288): at android.provider.Settings$System.putIntForUser(Settings.java:1294)
01-25 18:43:10.599: E/AndroidRuntime(1288): at android.provider.Settings$System.putInt(Settings.java:1288)
01-25 18:43:10.599: E/AndroidRuntime(1288): at opti.mize.omega.pModes.setActions(pModes.java:90)
01-25 18:43:10.599: E/AndroidRuntime(1288): at opti.mize.omega.pModes$1.onClick(pModes.java:77)
01-25 18:43:10.599: E/AndroidRuntime(1288): at android.view.View.performClick(View.java:4475)
01-25 18:43:10.599: E/AndroidRuntime(1288): at android.view.View$PerformClick.run(View.java:18786)
01-25 18:43:10.599: E/AndroidRuntime(1288): at android.os.Handler.handleCallback(Handler.java:730)
01-25 18:43:10.599: E/AndroidRuntime(1288): at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 18:43:10.599: E/AndroidRuntime(1288): at android.os.Looper.loop(Looper.java:137)
01-25 18:43:10.599: E/AndroidRuntime(1288): at android.app.ActivityThread.main(ActivityThread.java:5419)
01-25 18:43:10.599: E/AndroidRuntime(1288): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 18:43:10.599: E/AndroidRuntime(1288): at java.lang.reflect.Method.invoke(Method.java:525)
01-25 18:43:10.599: E/AndroidRuntime(1288): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
01-25 18:43:10.599: E/AndroidRuntime(1288): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
01-25 18:43:10.599: E/AndroidRuntime(1288): at dalvik.system.NativeStart.main(Native Method)
如果有人能指出我所缺少的东西,我将感激不尽。
答案 0 :(得分:1)
试试这段代码:
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, newbright);
您的代码中的cr
可能为空