0

我正在做一个项目,要求在应用程序中显示最大规模的地震。我遇到的问题是 for 循环使应用程序崩溃。如果我取出 for 循环,我会显示最低震级的地震。

我已经检查了 logcat,但它没有给我任何关于为什么的线索。所以我所要求的我想要一些帮助来指出我解决这个问题的方向。

谢谢你的帮助

这是代码

private void DisplayInfo(String result){

    try{
        JSONObject jsonObject = new JSONObject(result);
        JSONArray arr = new JSONArray(jsonObject.getString("earthquakes"));

        for (int a = 0; a<arr.length(); a++){

            JSONObject element = arr.getJSONObject(a);
            //Toast.makeText(getBaseContext(), element.getString("magnitude"), Toast.LENGTH_LONG).show();
            int[] magnitude = new int[]{0};
            Arrays.sort(magnitude);
            int max = magnitude[magnitude.length - 1];

            for(int i =0; i < magnitude.length; i++){
                if(magnitude[i] > max){
                    max = magnitude[i];

                  //  Toast.makeText(getBaseContext(), element.getString("hello World") + max, Toast.LENGTH_LONG).show();

           String value;
            value = "Country Name: " + element.getString("src")
                    + ("\n") + "Date & Time: " + element.getString("datetime")
                    + ("\n") +  "Magnitude: " +  element.getString("magnitude");

            TextView text = new TextView (this);
            text.setText(value);

            setContentView(text);
            Log.d ("String Value >>>>>", value);
        }
         }
            }

    }

Logcat 从按钮单击

01-09 00:09:45.868: D/Button Click ok(1383): http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo
01-09 00:09:47.928: D/dalvikvm(1383): GC_FOR_ALLOC freed 203K, 13% free 2799K/3200K, paused 35ms, total 38ms
01-09 00:10:05.738: E/SoundPool(278): error loading /system/media/audio/ui/Effect_Tick.ogg
01-09 00:10:05.738: W/AudioService(278): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-09 00:10:05.738: E/SoundPool(278): error loading /system/media/audio/ui/Effect_Tick.ogg
01-09 00:10:05.738: W/AudioService(278): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-09 00:10:05.738: E/SoundPool(278): error loading /system/media/audio/ui/Effect_Tick.ogg
01-09 00:10:05.748: W/AudioService(278): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-09 00:10:05.758: E/SoundPool(278): error loading /system/media/audio/ui/Effect_Tick.ogg
01-09 00:10:05.758: W/AudioService(278): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-09 00:10:05.758: E/SoundPool(278): error loading /system/media/audio/ui/Effect_Tick.ogg
01-09 00:10:05.768: D/Button Click ok(1383): http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo
01-09 00:10:05.778: W/AudioService(278): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-09 00:10:05.778: E/SoundPool(278): error loading /system/media/audio/ui/KeypressStandard.ogg
01-09 00:10:05.778: W/AudioService(278): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
01-09 00:10:05.778: E/SoundPool(278): error loading /system/media/audio/ui/KeypressSpacebar.ogg
01-09 00:10:05.808: W/AudioService(278): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
01-09 00:10:05.808: E/SoundPool(278): error loading /system/media/audio/ui/KeypressDelete.ogg
01-09 00:10:05.808: W/AudioService(278): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
01-09 00:10:05.808: E/SoundPool(278): error loading /system/media/audio/ui/KeypressReturn.ogg
01-09 00:10:05.838: W/AudioService(278): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
01-09 00:10:05.848: W/AudioService(278): onLoadSoundEffects(), Error -1 while loading samples

4

1 回答 1

0

也许你应该粘贴你的崩溃信息。
如果你捕获了异常,你应该检查异常信息。或者只为 JSON 添加 try catch 块,不要包括所有代码。
最后你可以添加一个断点,使用逐步调试代码。我想你应该注意类型转换和 element.getString(is null?)。

于 2016-01-09T05:03:53.887 回答