应用程序后,很长一段时间的背景下进行恢复时崩溃很长、应用程序、背景下

由网友(何人一笑)分享简介:闲置了很长的时间在后台后,我的应用程序崩溃。我调试,发现它崩溃的原因是由于 NullPointerException异常。例外的是由于这样的事实,在应用单例类数据是由垃圾收集器破坏时的应用是在背景。我现在用我的整个应用程序中的每个活动的静态数据。My application crashes after idling...

闲置了很长的时间在后台后,我的应用程序崩溃。我调试,发现它崩溃的原因是由于 NullPointerException异常。例外的是由于这样的事实,在应用单例类数据是由垃圾收集器破坏时的应用是在背景。我现在用我的整个应用程序中的每个活动的静态数据。

My application crashes after idling for a long time in the background. I debugged and found the reason that it is crashing is due to a NullPointerException. Exception was due to the fact that the data in the application singleton class is destroyed by the garbage collector when application is in background. I am using static data in each activity throughout my application.

我的问题是:有没有办法在后台的时候,使应用程序类的数据持久化?或者有没有其他的解决办法吗?

My question is: is there any way to make the data of application class persistent when in background? Or is there any other solution?

推荐答案

有关更准确的答案你把你的code在这里。以及Android设备内存有限,因此虚拟机可以删除任何一块$ C $的C中,它认为非必要的。

For more accurate answer you put your code here. On, Android memory is limited so VM can remove any piece of code it think un-necessary.

。看看活动生命周期的方法,特别是进入 onResume ,并确保你明白,完美。所以很多时候只是应用不使用活动的生命周期方法得当崩溃。

Look into Activity life-cycle method, specially into onResume and make sure that you understand that perfectly. So many time application crashes just for not using Activity life-cycle method properly.

另一个重要的设计考虑的活动是,不管发生什么事与持久性数据,你的活动应该显示其用户界面与一些默认值。因此,假设是这样的,如果我有我的数据将显示,如果我不这样做,我真的不在乎的。 您的UI永远也不会崩溃,或者没有数据。您可以使用 String.xml 用于存储一些默认值,甚至布局。

Another important design consideration for Activity is, no matter what happened with persistence data you Activity should display its UI with some default value. So assumption is like this, if i have data i will display if i don't, i really do not care. Your UI should never ever crash with or without data. You can use String.xml for storing some default value or even in layouts.

如果你还愿意去与单件类,这是完全正常的,但你一定要做以下检查每个试图访问你单​​身的时间。

if you still want go with singleton class, which is perfectly fine but make sure you do the following checking every time you try to access your singleton.

if (instance==null)
    instance=Singleton.getInstance()

你的的getInstance()方法不仅返回你当前的情况下,也将确保

your getInstance() method not only return you current instance it will also make sure that

在它初始化所有的对象和变​​量 其他特殊方法的实例方法

不要静态地从一个活动访问数据到另一个。这是不好的机器人专门为类型的问题,你现在面对并且也没有很好的面向对象的编程习惯。

Do not statically access data from one Activity to another. It is not good for android specially for the type problem you are facing now and also it is not very good OOP programming practice.

Shared$p$pference是坚持数据,如果符合您的要求为它去好办法。

SharedPreference is good way to persist data, if that meet your requirement go for it.

如果您想从喜欢的活动,服务或BroadcastReciever不同的Andr​​oid组件传递数据,你可以把它放在包内,发送的意图。而且,他们一直在为SQLLite数据存储,文件IO等等等等。

if you want to pass data from different Android component like Activity, Service or BroadcastReciever you can put it inside a bundle and and send as intent. And, as always their are SQLLite data storage, file IO etc etc.

阅读全文

相关推荐

最新文章