-1

I am using a normal loop to extract json infos from a file and putting them in a normal array adapter everything is purfectly set up but I always have a crash when I run the app, I revised all the code and I had no errors. Here is the code:

If needed I can include other code.

private QueryUtils() {
}

/**
 * Return a list of {@link Earthquake} objects that has been built up from
 * parsing a JSON response.
 */
public static ArrayList<Earthquake> extractEarthquakes() {

    // Create an empty ArrayList that we can start adding earthquakes to
    ArrayList<Earthquake> earthquakes = new ArrayList<>();

    // Try to parse the SAMPLE_JSON_RESPONSE. If there's a problem with the way the JSON
    // is formatted, a JSONException exception object will be thrown.
    // Catch the exception so the app doesn't crash, and print the error message to the logs.
    try {

        // Create a JSONObject from the SAMPLE_JSON_RESPONSE string
        JSONObject baseJsonResponse = new JSONObject(SAMPLE_JSON_RESPONSE);

        // Extract the JSONArray associated with the key called "features",
        // which represents a list of features (or earthquakes).
        JSONArray earthquakeArray = baseJsonResponse.getJSONArray("features");

        // For each earthquake in the earthquakeArray, create an {@link Earthquake} object
        for (int i = 0; i < earthquakeArray.length(); i++) {

            // Get a single earthquake at position i within the list of earthquakes
            JSONObject currentEarthquake = earthquakeArray.getJSONObject(i);

            // For a given earthquake, extract the JSONObject associated with the
            // key called "properties", which represents a list of all properties
            // for that earthquake.
            JSONObject properties = currentEarthquake.getJSONObject("properties");

            // Extract the value for the key called "mag"


            // Extract the value for the key called "place"
            String location = properties.getString("place");
            String magnitude = properties.getString("mag");
            // Extract the value for the key called "time"
            String time = properties.getString("time");

            // Extract the value for the key called "url"


            // Create a new {@link Earthquake} object with the magnitude, location, time,
            // and url from the JSON response.
            Earthquake earthquake = new Earthquake(magnitude, location, time);

            // Add the new {@link Earthquake} to the list of earthquakes.
            earthquakes.add(earthquake);
        }

    } catch (JSONException e) {
        // If an error is thrown when executing any of the above statements in the "try" block,
        // catch the exception here, so the app doesn't crash. Print a log message
        // with the message from the exception.
        Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
    }

    // Return the list of earthquakes
    return earthquakes;
}

}

this is the error message :

E/AndroidRuntime: FATAL EXCEPTION: main
          Process: com.example.android.quakereport, PID: 8512
          android.content.res.Resources$NotFoundException: Resource ID 
#0x0

the logcat image the error image

apparently the error is not a json parsing error it is a ressource not found from the getview method so I added this code in form of images because I couldn't ident them: the first part of the code the secound part of the code

  • 3
    Please add the crash report to the question – LS_ Jul 11 '18 at 10:18
  • 2
    Well which exception is thrown that lets your app crash? Look in the logcat. Post the relevant stacktrace. – greenapps Jul 11 '18 at 10:18
  • Taking no account on missing error stack trace. Did you considered using any library (like GSON or Moshi) for parsing json data? Using it can help you to avoid doing many thing by yours hands. In other words it can be harder to do something wrong – Bartosz Jul 11 '18 at 10:22
  • 2
    If you are not familar with logcat and stack traces, you can have a look at [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this). Even if the code is technically fine, it's possible that the JSON does't have the arrays or objects that you are asking for. That's where the [has() method](https://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)) is handy as you can first check is e.g. "features" or "properties" actually exist in the JSON object. – Markus Kauppinen Jul 11 '18 at 11:19
  • first yes I checked the logcat and I didn't understand nothing and secound I revised more than 12 times the json code (there is the link if you want) and everything looks fine. waitin... – Oussama Hassini Jul 11 '18 at 16:22
  • I added the crash report and I didn't understand @Bartosz comment – Oussama Hassini Jul 11 '18 at 16:34
  • and ps I m not that dumb to post that type of question – Oussama Hassini Jul 11 '18 at 16:36
  • @OussamaHassini, exception is throwing from EarthQuakeAdapter.java:39, your getView() method. Can you post your EarthQuakeAdapter code? – Amit K. Saha Jul 11 '18 at 16:38
  • from the stacktrace you posted here, it doesn't seem this is a json parsing issue at all. – Amit K. Saha Jul 11 '18 at 16:39
  • The error log also says the problem is that a resource is not found as it's a [Resources.NotFoundException](https://developer.android.com/reference/android/content/res/Resources.NotFoundException). – Markus Kauppinen Jul 11 '18 at 16:54
  • Thank you sorry I didn't knew the origin of the error I will add the get view code – Oussama Hassini Jul 11 '18 at 19:39
  • @OussamaHassini I meant that you may use external for parsing JSON, which is in many cases is very convenient and in some ways allows you to avoid some problems. Also I can give you another protip, If you don't understand stack trace of error, you can easily find line of error by clicking [blue link](https://ibb.co/cafuCT) in stack trace,. – Bartosz Jul 12 '18 at 05:29
  • Than you for help and I added the GET VIEW CODE hope you can find an issue @AmitK.Saha – Oussama Hassini Jul 12 '18 at 08:16
  • @OussamaHassini, check my answer, let me know if it works. – Amit K. Saha Jul 13 '18 at 14:48

1 Answers1

0

remove return super.getView(position,convertView,parent); you are doing it wrong, you don't need to return/call getView() if the convertView is null, you just to need to inflate it. I would suggest you to lookup for tutorials regarding custom ArrayAdapter.

Amit K. Saha
  • 5,871
  • 2
  • 27
  • 35