【问题标题】:static variables become null after incorporating v4 library合并 v4 库后静态变量变为空
【发布时间】:2013-03-12 09:59:12
【问题描述】:

我最近将v4 support lib 合并到我的Android 应用程序中,以便在标签之间滑动。我观察到静态对象变为空的应用程序中的随机崩溃。

我浏览了以下帖子,这些帖子告诉我内存管理的故事,其中 Android 杀死进程以声明内存,重新启动进程并重新启动最新活动。

我也明白这就是为什么静态变量不是存储应用程序数据的好地方的原因。我正在研究偏好和数据库作为替代方案。但考虑到数据的复杂性,这需要一些时间。

已编辑:我没有将数据存储在活动中,而是使用具有静态属性的类来保留应用程序级别的一组配置。

我的问题是,为什么我在集成 v4 支持库后会遇到这个问题?有没有人遇到过 support v4 lib 的类似问题?这与我的问题或我的模糊假设有什么关系吗?

Can static variable value be nulled by system in android app?

Android static object lifecycle

static variable null when returning to the app


这是我的应用程序崩溃时的日志。我在 ApplicationConfiguration: getListOrder() 方法中观察到 NPE,它引用了其中一个静态变量。日志中是否有任何迹象表明系统已终止该进程并重新启动它?或者它们表示不同的东西?

!@Sync 406D/HierarchicalStateMachine(287):handleMessage:E msg.what=83D/HierarchicalStateMachine(287): processMsg: ConnectedStateD/WifiStateMachine(287):ConnectedState{什么=83 当=-4ms arg1=5 }D/HierarchicalStateMachine(287):handleMessage: XW/PowerManagerService(287):定时器 0x3->0x3|0x0D/dalvikvm(2775): GC_EXPLICIT 释放 61K,5% 释放 6209K/6531K,暂停 6ms+2msI/fSharp:BackgroundServiceHandler(4637): 收到消息: 3I/fSharp:BackgroundServiceHandler(4637):列出消息处理程序 存在,发送消息I/fSharp:BackgroundThread(4637): Completed 处理出站队列I / fSharp:BackgroundThread(4637):在做 循环...D/AndroidRuntime(4637):关闭 VMW/dalvikvm(4637): threadid=1:线程以未捕获的异常退出 (组=0x401b7760)E/AndroidRuntime(4637):致命异常: mainE/AndroidRuntime(4637): java.lang.NullPointerExceptionE/AndroidRuntime(4637):在 com.tfs.fsharpsupport.common.configuration.ApplicationConfiguration.getListOrder(ApplicationConfiguration.java:125)E/AndroidRuntime( 第4637章 com.tfs.fsharp.model.ListModel.refreshList(ListModel.java:576)E/AndroidRuntime( 第4637章 com.tfs.fsharp.model.ListModel.onListChanged(ListModel.java:332)E/AndroidRuntime( 第4637章 com.tfs.fsharp.model.ListModel.handleListMessage(ListModel.java:285)E/AndroidRuntime( 第4637章 com.tfs.fsharpsupport.background.BackgroundServiceHandler.handleMessage(BackgroundServiceHandler.java:104)E/AndroidRuntime( 第4637章 android.os.Handler.dispatchMessage(Handler.java:99)E/AndroidRuntime( 4637): 在 android.os.Looper.loop(Looper.java:132)E/AndroidRuntime( 第4637章 android.app.ActivityThread.main(ActivityThread.java:4028)E/AndroidRuntime( 4637): 在 java.lang.reflect.Method.invokeNative(Native 方法)E/AndroidRuntime(4637):在 java.lang.reflect.Method.invoke(Method.java:491)E/AndroidRuntime( 第4637章 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)E/AndroidRuntime( 第4637章 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)E/AndroidRuntime( 4637): 在 dalvik.system.NativeStart.main(Native 方法)W/ActivityManager(287):强制完成活动 com.tfs.fsharp.ui.activity/.StartupActivityE/android.os.Debug(287): 转储状态 > /data/log/dumpstate_app_errorI/dumpstate(5407): beginD/HierarchicalStateMachine(287)

【问题讨论】:

  • 可能您的 apk 现在更大了,因此可能会遇到内存问题,尤其是当您的静态变量中有大量数据时。但是,日志应指示终止和重新启动。
  • 您能否查看以下日志并告诉我该服务是否已被终止?

标签: android performance


【解决方案1】:

显然,当您切换标签时,您的活动在幕后被杀死。希望你知道life cycle of the Activity

就您的信息而言,在活动中使用静态变量 delcares 并不是一个好习惯。你会经常遇到Null pointer exceptions。如果您坚持使用静态变量而不是共享偏好或内容提供者,那么我认为您应该遵循以下方法。

我曾经遇到过同样的问题,我声明了一个单独的类并将其用于静态变量。通过对 Manifest 文件添加更多行和更改,您可以将它们设为全局并在应用程序范围内使用它。

Check out this tutorial on how to do this.

【讨论】:

    猜你喜欢
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多