0

我无法理解为什么我在播放我的音乐游戏中10首歌曲中的一首时收到IndexOutOfBound异常。 我认为,即使使用asynctask.get()等待计算完成,不知何故,他跳过了10个歌曲信息(dieciCanzoni)列表的填充。在Asynctask期间Android Indexoutofbounds异常

你能帮助我吗?

谢谢!

logcat的:

11-26 10:29:49.441: E/AndroidRuntime(7509): FATAL EXCEPTION: main 
11-26 10:29:49.441: E/AndroidRuntime(7509): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 
11-26 10:29:49.441: E/AndroidRuntime(7509):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 
11-26 10:29:49.441: E/AndroidRuntime(7509):  at java.util.ArrayList.get(ArrayList.java:304) 
11-26 10:29:49.441: E/AndroidRuntime(7509):  at com.guessthesetunes.Canzone.onCompletion(Canzone.java:539) 
11-26 10:29:49.441: E/AndroidRuntime(7509):  at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1528) 
11-26 10:29:49.441: E/AndroidRuntime(7509):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-26 10:29:49.441: E/AndroidRuntime(7509):  at android.os.Looper.loop(Looper.java:137) 
11-26 10:29:49.441: E/AndroidRuntime(7509):  at android.app.ActivityThread.main(ActivityThread.java:4575) 
11-26 10:29:49.441: E/AndroidRuntime(7509):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-26 10:29:49.441: E/AndroidRuntime(7509):  at java.lang.reflect.Method.invoke(Method.java:511) 
11-26 10:29:49.441: E/AndroidRuntime(7509):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
11-26 10:29:49.441: E/AndroidRuntime(7509):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
11-26 10:29:49.441: E/AndroidRuntime(7509):  at dalvik.system.NativeStart.main(Native Method) 

我得到该线路上的错误,即调用初始化媒体播放器的方法: PlayStream(dieciCanzoni.get(ⅰ)获得(0));

中的AsyncTask的代码,我使用的是:

class Json extends AsyncTask<String, String, String> 
{ 
List<String> canzoni = new ArrayList<String>(5); 
ProgressDialog pDialog; 
int[] arrayGenere; 
Context context; 

public Json(int[] arrayGenere,Context context) 
{ 
    this.arrayGenere = arrayGenere; 
    this.context = context; 
} 

@Override 
protected void onPreExecute() 
{ 
    super.onPreExecute(); 
    pDialog = new ProgressDialog(context); 
    pDialog.setMessage("Preparazione Round..."); 
    pDialog.setIndeterminate(false); 
    pDialog.setCancelable(false); 
    pDialog.show(); 
} 

protected String doInBackground(String... params) 
{ 
    try 
    {  
     int randomLookupId; 
     JSONObject obj;       
     JSONArray jsonArray;  

     for(int i = 0; i < 10; i++) 
     { 
      canzoni = new ArrayList<String>(); 

      obj = getJSONObject(scegliClassifica(arrayGenere)); 
      jsonArray = obj.getJSONArray("resultIds"); 

      randomLookupId = new Random().nextInt(jsonArray.length()); 

      JSONObject finalObj = getJSONObject("http://itunes.apple.com/lookup?id="+jsonArray.getString(randomLookupId)); 
      JSONArray finalJsonArray = finalObj.getJSONArray("results");  

      JSONObject returnObj = finalJsonArray.getJSONObject(0); 

      canzoni.add(returnObj.getString("previewUrl")); 
      canzoni.add(returnObj.getString("artistName")); 
      canzoni.add(returnObj.getString("trackName")); 
      canzoni.add(returnObj.getString("artistViewUrl")); 
      canzoni.add(returnObj.getString("artworkUrl100")); 

      Canzone.dieciCanzoni.add(i, new ArrayList<String>(canzoni)); 
     } 
    } 
    catch (JSONException ignored) 
    { 
     ignored.getCause(); 
    } 
    catch (MalformedURLException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return null; 
} 

private String scegliClassifica(int[] arrayGenere) 
{ 
    int randomArrayPosition = new Random().nextInt(arrayGenere.length); 
    return "http://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/charts?cc=us&g="+arrayGenere[randomArrayPosition]+"&name=Songs&limit=300"; 
} 

@Override 
protected void onPostExecute(String stringa) 
{ 
    pDialog.dismiss(); 
} 

JSONObject getJSONObject(String url) throws IOException, MalformedURLException, JSONException 
{ 

    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); 

    InputStream in = conn.getInputStream(); 

    try 
    { 
     StringBuilder sb = new StringBuilder(); 
     BufferedReader r = new BufferedReader(new InputStreamReader(new DoneHandlerInputStream(in))); 
     for (String line = r.readLine(); line != null; line = r.readLine()) 
     { 
      sb.append(line); 
     } 
     return new JSONObject(sb.toString()); 
    } 
    finally 
    { 
     in.close(); 
    } 
} 
} 

这是调用的AsyncTask:

try { 
     new Json(arrayGenere,Canzone.this).execute().get(15000,TimeUnit.MILLISECONDS); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (TimeoutException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

编辑:

改变JsonArray的大小后到JsonArray.lenght() - 1,它也不工作! 例如,在一种情况下,Json类只填充了List的两个元素,但我不明白为什么!

你能帮我吗? 谢谢

+2

变化'randomLookupId =新的随机()nextInt(jsonArray.length());'到randomLookupId =新的随机()。 nextInt(jsonArray.length() - 1); – thepoosh

+0

你能标记你的代码中出现错误的那一行吗?这会让事情变得更容易。但很可能你的arrayGenere是空的。在尝试访问其中的项目之前,您可以验证它的大小吗? – TofferJ

+0

arrayGenere总是被填充,因为我根据用户选择的流派传递了int []的其他初始化数组的值。 –

回答

0

在你身上doInBackground方法你怎么知道你是在“法律规模”?

你的整个方法建立在错误/硬编码的假设......

+0

你能解释更好吗?无论如何,我没有发布其他类,但我知道尺寸,因为我选择了一个基于数组长度的随机值! –

+0

JSONObject returnObj = finalJsonArray.getJSONObject(0); 如何确保finalJsonArray中有1个对象? 此外,为什么你添加名称最终到您的变量,而不使他们最终像'最终的JSONObject finalJSONObject = new ....'? –

+0

我敢肯定,有一个对象,因为我检索一个随机的歌曲ID与上面的链接。所以它总是处于0的位置。对于'最终'的东西,这只是一个语言问题,我不知道如何命名它......创造力的丧失;) –

1

记住数组的第一个元素是索引为0这样的长度会比最高指数走高1,所以你需要减去1从您的randomLookupId使用它之前。

(考虑与长度为1的阵列,应当始终随机选择索引0)

+0

好吧,像@thepoosh在评论中说。但现在logcat给了我另一个错误:randomLookupId = new Random()。nextInt(jsonArray.length() - 1); \t非法结果例外 –

+0

尝试 'randomLookupId = new Random()。nextInt((jsonArray.length() - 1));'' –