0

i am Building My College project on Battery Saving. uptill now everything is working fine but the problem is arising when i try to set the brightness through onClick Method using a Button here is the code :

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

And then i get a Null pointer exception on the brightness code.

am i missing something ? this Button isnt in a Activity,but in a BaseAdapter class Which generates n no of buttons if they exists.

The errors i get are :

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)

If anyone can point out what I am missing I will be thankful.

Ignacio Correia
  • 3,611
  • 8
  • 39
  • 68

1 Answers1

1

Try this code:

android.provider.Settings.System.putInt(getContentResolver(),
    android.provider.Settings.System.SCREEN_BRIGHTNESS, newbright);

Maybe cr in your code is null

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • tried it at first place. But getContentResolver() cant be recognised. The reason behind might be because i am using the method in a BaseAdapter class and not a Activity class i.e[ my class is :- Mode extends BaseAdapter || and not Mode extends Activity] so my class cant have onCreate(Bundle) Method. – Galacticpunk Jan 26 '14 at 11:48
  • well, then pass Context to your BaseAdapter: http://stackoverflow.com/questions/16594557/how-to-get-context-for-baseadapter-in-android – marcinj Jan 26 '14 at 12:07
  • i got Another way Thanks alot. – Galacticpunk Jan 26 '14 at 12:29