【问题标题】:android for loop magnitude arrayandroid for循环幅度数组
【发布时间】:2016-04-13 21:18:36
【问题描述】:

我正在做一个项目,要求在应用程序中显示最大规模的地震。我遇到的问题是 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

【问题讨论】:

  • 你能用帖子复制LogCat数据吗

标签: android arrays for-loop netbeans-8.1


【解决方案1】:

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

【讨论】:

  • 你说得对,我的 for 循环返回 null。如果我对数组进行硬编码并删除 for 循环,它将找到最大的双精度数。那么为什么要让 for 循环工作我被卡住了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2021-01-12
  • 1970-01-01
  • 2018-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多