简体   繁体   English

长时间处于后台时,应用程序丢失数据

[英]Application loosing data when in background for long period

I have an aplication in which I fill a listview from database (greendao). 我的应用是从数据库(greendao)填充列表视图。 I have screen orientation support, so it works in both orientations. 我有屏幕方向支持,因此可以在两个方向上使用。 The problem I have is that when app is the background for a longer period of time (cant find some certain point when this happens) and I go back to it, the listview is empty and it crashes if I try to do anything. 我的问题是,当应用程序长时间处于后台运行时(无法在发生这种情况时找到某个特定点),然后返回到该列表,列表视图为空,并且如果我尝试执行任何操作都会崩溃。

I am having an impossible time debugging this since it happens only when application is in the background for some time. 我无法调试该调试器,因为它仅在应用程序在后台运行一段时间时才会发生。

I fill the listview by filling an arraylist from database and then sending it to the adapter. 我通过填充数据库中的arraylist并将其发送到适配器来填充listview。

Does anyone have an idea why this is happening? 有谁知道为什么会这样?

EDIT: broadcast reciver problem solved 编辑:广播接收器问题解决

But the reason why listviews are empty is still not found. 但是,仍然找不到列表视图为空的原因。

I got another logcat tho (the problem is getting data from database :/ ): 我得到另一个logcat tho(问题是从数据库:/获取数据):

11-11 13:45:31.136: E/AndroidRuntime(4468): FATAL EXCEPTION: main
11-11 13:45:31.136: E/AndroidRuntime(4468): java.lang.NullPointerException
11-11 13:45:31.136: E/AndroidRuntime(4468):     at si.comtron.tronpos.content.DatabaseHelpers.viewArticleWithPrice(DatabaseHelpers.java:70)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at si.comtron.tronpos.ArticlesFragment.onCreateView(ArticlesFragment.java:269)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:831)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1037)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.app.BackStackRecord.run(BackStackRecord.java:635)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1399)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:428)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.os.Handler.handleCallback(Handler.java:615)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.os.Looper.loop(Looper.java:155)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.app.ActivityThread.main(ActivityThread.java:5511)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at java.lang.reflect.Method.invokeNative(Native Method)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at java.lang.reflect.Method.invoke(Method.java:511)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at dalvik.system.NativeStart.main(Native Method)

Go to Developer Options on your phone and check the checkbox Don't save Activities . 转到手机上的Developer Options ,然后选中Don't save Activities复选框。 This way any of your Activities gets killed as soon as it's in background and you, most probably, will be able to recreate the issue without waiting. 这样,您的任何“ Activities只要在后台就被杀死,您很可能将能够重现问题而无需等待。

IntentReceiver si.comtron.tronpos.USB.USBService$1@4197ac28 that was originally registered here. IntentReceiver si.comtron.tronpos.USB.USBService$1@4197ac28最初在此处注册。 Are you missing a call to unregisterReceiver()? 您是否缺少对unregisterReceiver()的调用?

It seems you are using BroadcastReceiver 看来您正在使用BroadcastReceiver

Unregister your receiver in onPause() : onPause()注销接收者:

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(yourReceiver);
}

Does anyone have an idea why this is happening? 有谁知道为什么会这样?

Your app's process was terminated due to low memory conditions, but it still existed in the recent-tasks list. 您的应用进程由于内存不足而终止,但它仍存在于“近期任务”列表中。 When you try returning to the app, Android forks a fresh process for you and starts up the last activity that you were on... and you are assuming something else in the process should already be around, when it is not. 当您尝试返回该应用程序时,Android为您创建了一个新的流程,并启动了您上一次进行的活动...并且您假设流程中的其他内容已经存在,而实际并非如此。

您可以创建一个包含所有数据的文件,在活动的OnCreate中,您应检查变量是否包含数据。如果没有,则可以从以前保存的文件中提取数据。

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

相关问题 LiveData Room 在进入后台时丢失数据 - LiveData Room loosing data when going to background 为什么长时间在后台运行时,我的android应用程序的所有静态变量都会被破坏? - Why my android application's all static variables are destroyed when the app is in background for a long period of time? 当应用程序长时间在后台停留时,它会崩溃 - Application crashes when it remains in background for long time 当应用程序长时间进入后台时,片段未绑定到父对象 - Fragment is not attched to parent when application goes to background for long time 在背景上失去MainActivity - Loosing MainActivity on Background Android应用程序在后台运行时保存数据 - Android Application saving data when app in background 当我的应用程序进入后台时清除数据 - Clearing the data when my Application goes to background 我正在做一个简单的清单应用程序。我能够将数据保存到文件中,但是当我关闭应用程序并重新打开时,我正在丢失数据 - I'm making a simple to do list application.I am able to save the data to file but when I close the application and reopen i'm loosing the data Android的应用程序主线程在不降低性能的情况下可以忙多长时间 - How long Android's application main thread can be busy without loosing performance 再次进行相同的活动时丢失 recyclerview 数据 - Loosing recyclerview data when going again on same activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM