1

I have an aplication in which I fill a listview from database (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.

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 :/ ):

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)
Tadej Vengust
  • 1,351
  • 4
  • 18
  • 35

4 Answers4

1

Go to Developer Options on your phone and check the checkbox 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.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
1

IntentReceiver si.comtron.tronpos.USB.USBService$1@4197ac28 that was originally registered here. Are you missing a call to unregisterReceiver()?

It seems you are using BroadcastReceiver

Unregister your receiver in onPause():

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(yourReceiver);
}
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
  • This helped with the reciver problem but this was not the cause of my actual problem. – Tadej Vengust Nov 11 '14 at 12:49
  • Eaven tho it crashed once more after using this I cannot recreate the problem since. So for the time beeing I will mark this as the answer and do some testing. – Tadej Vengust Nov 11 '14 at 13:05
0

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • That is an interesing point but how can I avoid this? Or atleast force application to open from the start if this happens? – Tadej Vengust Nov 11 '14 at 12:21
  • @TadejVengust: "That is an interesing point but how can I avoid this?" -- you cannot avoid this. "Or atleast force application to open from the start if this happens?" -- the better answer is to write your app to handle this. Your argument is akin to having a Web site that refuses to support inbound links to anything other than the home page. In cases where the users might find your behavior justifiable, detect that you are missing some data, and use `startActivity()` to route the user to the proper spot (plus `finish()` so they cannot BACK up). – CommonsWare Nov 11 '14 at 12:40
  • The weirdest this is that If I am in debug mode, this never happens and i have Don't save Activities enabled. But if I run it normaly it happens. And the problems is located when I try to get data from sqlite. – Tadej Vengust Nov 11 '14 at 13:52
  • You said I should use startActivity(). How can I do that from fragment for MainActivity? – Tadej Vengust Nov 11 '14 at 14:53
  • 1
    @TadejVengust: Call a method on `MainActivity` that does the work. Or, call `getActivity().startActivity()`. – CommonsWare Nov 11 '14 at 15:27
  • This did not work but it have me an idea and I fixed my problem using: http://stackoverflow.com/questions/6609414/howto-programatically-restart-android-app – Tadej Vengust Nov 12 '14 at 08:13
0

You can create a file that contains all your data, In OnCreate of your activity you should check whether your variables have data.If not , you can just pull the data from the files that you have saved previously.

Payal
  • 903
  • 1
  • 9
  • 28