0

I am trying to fetch the screen brightness in Android.

The scenario is such that Class A calls a function of Class B as:

Class A:

if(condition is true){
  ClassB.function();
}

Class B:

function(){
WindowManager.LayoutParams screenBrightness = getWindow()
                    .getAttributes();
screenBrightness.screenBrightness = 1;
screenBrightness.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
screenBrightness.flags |= WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
screenBrightness.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
getWindow().setAttributes(screenBrightness);
}

The exception received is as:

06-15 13:36:30.476: E/AndroidRuntime(8873): FATAL EXCEPTION: main
06-15 13:36:30.476: E/AndroidRuntime(8873): java.lang.NullPointerException
06-15 13:36:30.476: E/AndroidRuntime(8873):     at function(B.java:68)
06-15 13:36:30.476: E/AndroidRuntime(8873):     at something.run(A.java:104)
06-15 13:36:30.476: E/AndroidRuntime(8873):     at android.os.Handler.handleCallback(Handler.java:587)
06-15 13:36:30.476: E/AndroidRuntime(8873):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-15 13:36:30.476: E/AndroidRuntime(8873):     at android.os.Looper.loop(Looper.java:130)
06-15 13:36:30.476: E/AndroidRuntime(8873):     at android.app.ActivityThread.main(ActivityThread.java:3701)
06-15 13:36:30.476: E/AndroidRuntime(8873):     at java.lang.reflect.Method.invokeNative(Native Method)
06-15 13:36:30.476: E/AndroidRuntime(8873):     at java.lang.reflect.Method.invoke(Method.java:507)
06-15 13:36:30.476: E/AndroidRuntime(8873):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
06-15 13:36:30.476: E/AndroidRuntime(8873):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
06-15 13:36:30.476: E/AndroidRuntime(8873):     at dalvik.system.NativeStart.main(Native Method)

Has anyone faced this issue earlier?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Amrit
  • 2,295
  • 4
  • 25
  • 42
  • what is line 68 @ `B`? – Shayan Pourvatan Jun 15 '14 at 08:28
  • @shayanpourvatan Its "WindowManager.LayoutParams screenBrightness = getWindow().getAttributes();". After debugging further I got to know that getWindow() is returning null hence the error. But I am confused as Class B activity is visible on screen. – Amrit Jun 15 '14 at 08:30
  • both class A and class B is an activity class? you need create static method and pass context of activity to that function, – Shayan Pourvatan Jun 15 '14 at 08:32
  • @shayanpourvatan Yes, both A and B are activity class. You mean Class B should be created as static class. Any other alternatives than this? I tried using this.getWindow() but still the same issue, – Amrit Jun 15 '14 at 08:34

2 Answers2

1

I had this problem when I called transaction.commit().

After changing it to transaction.commitAllowingStateLoss() the problem went away.

Shadow
  • 8,749
  • 4
  • 47
  • 57
tommy
  • 11
  • 1
0

try following code:

if(condition is true){
  ClassB.function(this);
}

and function is:

public static void function(Activity activity){

    WindowManager.LayoutParams screenBrightness = activity.getWindow()
                    .getAttributes();
    screenBrightness.screenBrightness = 1;
    screenBrightness.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
    screenBrightness.flags |= WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
    screenBrightness.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
    activity.getWindow().setAttributes(screenBrightness);
}

if you want use this approach better to see This thread because pass reference of activity is not a good approach.

but as i see your problem is better to use SharedPreference to save brightness in Class A and use that in class B,

you can see This Question for understanding how to use SharedPreference in android, for fetch set and edit value.

if you have any question about that you can ask me.

Community
  • 1
  • 1
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
  • If we call "ClassB.function(this);" from Class A it will pass the context of Class A, right? How will it access the getWindow() of class B instead? Just curious, haven't tried it yet. – Amrit Jun 15 '14 at 08:39
  • i dont get you? you want to know how you must call that from class B? – Shayan Pourvatan Jun 15 '14 at 08:40
  • Just noticed that getWindow() will be accessible under Activity.class and its not defined under Context.class. Can you please check it? – Amrit Jun 15 '14 at 08:41
  • Its like I want to set the brightness of Class B. If I call that function from Class A it will change brightness of A and it won't reflect over B. Do you have any other suggestion to do it? – Amrit Jun 15 '14 at 08:47
  • you can use SP for saving value in Class A and set brightness in onResume() of class B, then all time that you comes to this class your brightness being refreshed. – Shayan Pourvatan Jun 15 '14 at 08:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55642/discussion-between-raj-and-shayan-pourvatan). – Amrit Jun 15 '14 at 08:50