2016-01-09 134 views
0

我正在做一個項目,要求在應用程序中顯示最高級別的地震。我遇到的問題是for循環崩潰了應用程序。如果我採取循環,我會得到顯示的最低震級。android for loop magnitude array

我檢查了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

+0

你可以複製LogCat數據與帖子 – Alok

回答

0

也許你應該粘貼崩潰信息。
如果發現異常,應該檢查異常info.Or只爲JSON添加try catch塊,不要包含所有代碼。
最後你可以添加一個斷點,一步步調試代碼。我認爲你應該注意到類型cast和element.getString(是空的?)。

+0

你是對的我的for循環返回null。如果我硬編碼數組,並刪除for循環,它會發現最大的雙。那麼爲什麼要讓for循環工作,我卡住了。 – warwick