简体   繁体   English

尝试从其他类检索时的空值

[英]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. 我有以下代码,batteryLevel在这里显示正确的值 - 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? 当我获得AlarmEventService.batteryLevel的值时,为什么在我的外部调用中值为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. 另一个名为batteryLevel的变量可以在本地声明,因此不会将值分配给全局版本。
  • the value of batteryLevel may be reset after your initial call. 初次通话后,batteryLevel的值可能会重置。

Make sure that the statement: 确保声明:

batteryLevel = String.valueOf(level); 

is actually being called before 实际上是被召唤之前

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从Firebase检索值,但获取空值? - Trying to retrieve value from Firebase, but getting null? 为什么在其他类(Java / apache吊索)中使用时,父类的字段为何具有空值 - Why does the field from parent class has a null value when using in other class (Java/apache sling) 尝试从方法检索数据时,变量返回null - Variable returns null when trying to retrieve data from a method 尝试从Swing中的JTable检索数据时,出现空指针异常 - Null Pointer Exception when trying to retrieve data from JTable in Swing 从 firebase 检索数据时,在 android App 中获取 null 值 - Getting null value in android App when data is retrieve from firebase 格式错误的表达式:尝试从哈希图检索值时出现“错误” - Malformed expression :“Error” when trying to retrieve value from hashmap 尝试从数据库检索密码值时出错 - errors when trying to retrieve a password value from a database 尝试检索PackageInfo时获取空上下文 - Getting Null Context when trying to retrieve PackageInfo 尝试检索JavaBean属性时,“null”和0值 - “null” and 0 values when trying to retrieve JavaBean properties 尝试从 java 类中获取值返回 null - Trying to get value from a java class returns null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM