简体   繁体   中英

Null value when trying to retrieve from other class

somehow my mind is not working and mild fever didn't help.

I have the following code, batteryLevel here shows the correct value - 50.

public class AlarmEventService extends Service {    
    static String batteryLevel; 
    ...
    int level = intent.getIntExtra("level", 0); 
    batteryLevel = String.valueOf(level); 
    Log.i(APP_TAG, batteryLevel);
}

Why in my outside call when I get value of AlarmEventService.batteryLevel the value is null?

public class AlarmEventReceiverWake extends BroadcastReceiver { 
    ...
    Log.i(APP_TAG, "Battery Level " + AlarmEventService.batteryLevel);
}

From what you have presented of the code, there is no reason why it shouldnt work as expected. It may be possibile that:

  • Another variable named batteryLevel may be declared locally, thereby not assigning the value to the global version.
  • the value of batteryLevel may be reset after your initial call.

Make sure that the statement:

batteryLevel = String.valueOf(level); 

is actually being called before

Log.i(APP_TAG, "Battery Level " + AlarmEventService.batteryLevel);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM