0

I am creating and app and so far everything was good. Today I was dealing with the data in a fragment to reach my webservice and return the json and then insert that data into my sqlite and after that show it in a listview and update the data second by second.

Everything was right until i find that if I click too fast in the bottom navigation where the items from the fragments are, it will crash the aplication while it is doing the transaction on the fragment. it only occurs when I click between that fragment and more 1 of the other 4.

So my question is, is there anyway I can deal with this problem? like if I click in another item from bottom navigation the transitions from the last item stops? if so how?

2 Answers2

3

As far as I can understand (with little info), the issue is mostly likely that the fragment is getting destroyed (when the code logic is trying to connect to the server and return the json data to the app) and you're trying to access the destroyed fragment elements (or items, here in this case, list view, to populate it with the received data).

Inside your each and every fragment, after the json data is received and before doing anything else, check if the fragment is still visible and attached to the activity and also the activity is not destroyed.

// Check the activity is not destroyed and the 
// fragment is still connected to the activity
if (getActivity() != null && isAdded()) {
    // Parse the JSON data
    // Write to your SQL database
    // Load the data into the list view
}
Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58
  • the thing is that solution would work if the connection to get the json was wrote in the fragment, but in the fragment the only thing I have is set the ListView R.id, set the adapter for the listview and then run the db for insert the values into the listview. after that inside CustomAdapter I run some functions each makes a connection to update the item values in the items. I will Edit my post with the code – José Emanuel Jul 07 '17 at 08:44
  • Could you post the stack trace too? – Srikar Reddy Jul 07 '17 at 10:10
  • already posted a print screen. the trace was getting clean 1s after the app crashed – José Emanuel Jul 07 '17 at 10:29
  • Your app is throwing NegativeArraySizeException. Put log statements and check if the ticketsObject or any other arraylist size is negative. Can't tell which file and at which line the crash is happening since only 6 lines of crash data was visible in the image. – Srikar Reddy Jul 08 '17 at 05:54
  • Logcat disappears - https://stackoverflow.com/questions/16817566/restore-logcat-window-within-android-studio NegativeArraySizeException - https://groups.google.com/forum/#!topic/volley-users/0W-oI6za8VY – Srikar Reddy Jul 08 '17 at 05:54
0

I found that 'compile 'com.android.volley:volley:1.0.0'' version of volley had this error and google (or the people that made the volley) didn't update volley in google library since then.

The way to work around this was work with a non-official version: 'compile 'com.mcxiaoke.volley:library:1.0.19''

This version had this "bugs" from volley already corrected. For the future if anyone wanna know more about this there is the link from mcxiaoke: https://github.com/mcxiaoke/android-volley

and a link to how to use it: https://www.thorntech.com/2016/03/parsing-json-android-using-volley-library/