Android静态变量丢失了

时间:2016-11-14 18:54:05

标签: java android garbage-collection

我使用的是只有静态变量的类来存储所有常量和运行时属性。但是,最近我看到当我引用这些变量的实例时,我得到了null。

以下是我的班级定义:

public class PlayerProperties {

    // Runtime but constant properties
    public static int screenHeight;
    public static int screenWidth;
    public static String androidId;

    // Static properties

    // Urls
    public static final String baseUrl = "http://www.blynq.in/api/player/";

    public static final String registerUrlAppender = "activationKeyValid";
    public static final String schedulesUrlAppender = "getScreenData";
    public static final String updateUrlAppender = "updateAvailable";
    public static final String tokenRegisterUrl = "fcmRegister";
    public static final String mediaStatsUrl = "mediaStats";
    public static final String logsUrl = "logs";
    public static final String pingUrl = "ping";
    public static final String screenInfoUrl = "screenInfo";

    // Developer Keys
    public static final String youtubeDeveloperKey = "ABCDEFGH...";

    // Folder structure
    public static final String mediaFolder = "player/media";
    public static final String imagesFolder = "player/media/images";
    public static final String videosFolder = "player/media/videos";
    public static final String pdfFolder = "player/media/pdf";
    public static final String gifFolder = "player/media/gif";
    public static final String webFolder = "player/media/web";
    public static final String othersFolder = "player/media/others";
    public static final String logsFolder = "player/logs";

    public static final String defaultFolder = "player/default/";
    public static final String serFolder = "player/ser/";
    public static final String tempFolder = "player/temp/";

    // Shared Prefs Keys
    public static final String ANDROID_ID_KEY = "ANDROID_ID";
    public static final String MY_PREFERENCES_KEY = "MyPrefs";


    // General properties
    public static final String dateTimeFormatString = "ddMMyyyyHHmmss";
    public static final String dateFormatString = "yyyy-MM-dd";

    // Timeouts
    public static final int httpPollTimeout = 20000;                    // in millis
    public static final int pingPeriodicity = 30;                       // in secs
    public static final int updateCheckPeriodicity = 24;                // in hrs
    public static final int pushEnabledPollPeriodicity = 30;            // in secs
    public static final int pushDisabledPollPeriodicity = 30;           // in secs
    public static final int statsUploadPeriodicity = 60;                // in mins
    public static final int logsUploadPeriodicity = 24;                 // in hours
    public static final int cleanupPeriodicity = 24;                    // in hours
    public static final int registrationStatusRecheckPeriod = 20000;    // in millis
    public static final int tokenResendToServerPeriod = 20000;          // in millis

    // Others
    public static final int maxTextHeight = 50; // in dp
    ...
}

我没有存储任何引用instantiatin PlayerProperties类,因为其中包含的所有变量都是静态的。

当我使用PlayerProperties.androidId引用变量androidId时,我有时会得到null。

我已在其中一项活动中初始化变量:

PlayerProperties.androidId = sharedpreferences.getString(PlayerProperties.ANDROID_ID_KEY, String.valueOf(UUID.randomUUID()));
  1. 我的怀疑是垃圾收集器被android踢了。如果gc确实启动了,它是否会取消我所有运行时初始化的静态变量?

  2. 哪个内存段是存储的静态变量?

  3. 如果没有,还有什么问题可以解决?

  4. 额外的详细信息:我的应用程序配置为在启动时自动启动。我只面对低端处理器以及应用程序在启动时自动触发时面临上述问题。

3 个答案:

答案 0 :(得分:1)

只有<div w3-include-html="menu.html"></div> 可以为null,当您想要使用它时,您应该在此类或public static String androidId;

中的其他类中初始化它

答案 1 :(得分:0)

androidId 是对字符串的引用,string是一个默认情况下会将空引用作为初始值的类,如果你不这样做的话....

看看这个来自oracle的doc

enter image description here

所以基本上 androidId 是空的,因为没有初始化....变量是静态的事实在这种情况下是不相关的......

有关详细信息,请参阅here

  

...我有时会变为空。

是的,除非它的值在运行时改变,否则你将始终为null ...

答案 2 :(得分:0)

请注意以下问题:

  1. 垃圾收集器仅删除未引用的对象。只有在运行时从JVM卸载静态变量时,静态变量才会丢失它们的值。

  2. 通过活动启动应用时会发生初始化。如果有任何服务/广播接收器正在访问其他类中的方法并使用这些ID,并且该应用程序尚未运行,则未初始化的值将被引用为默认值。如果是字符串 - 它为空。

  3. 与低端处理器无关,经常容易责怪它们,但JVM足够强大。