异步任务期间Android Indexoutofbounds异常
我不明白为什么我在复制音乐游戏中的10首歌曲时收到IndexOutofBond异常。 我认为,即使使用asynctask.get()等待计算完成,他也会以某种方式跳过10首歌曲信息的列表填充(Dieccanzoni) 你能帮我吗 谢谢大家! 日志:异步任务期间Android Indexoutofbounds异常,android,android-asynctask,media-player,indexoutofboundsexception,Android,Android Asynctask,Media Player,Indexoutofboundsexception,我不明白为什么我在复制音乐游戏中的10首歌曲时收到IndexOutofBond异常。 我认为,即使使用asynctask.get()等待计算完成,他也会以某种方式跳过10首歌曲信息的列表填充(Dieccanzoni) 你能帮我吗 谢谢大家! 日志: 11-26 10:29:49.441: E/AndroidRuntime(7509): FATAL EXCEPTION: main 11-26 10:29:49.441: E/AndroidRuntime(7509): java.lang.Index
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)
我在这一行得到了错误,它调用了一个初始化mediaplayer的方法:
PlayStream(dieccanzoni.get(i.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();
}
}
}
编辑:
将JsonArray的大小更改为JsonArray.lenght()-1后,它也不起作用!!!
例如,在一个例子中,类Json只填充了列表中的2个元素,但我不明白为什么
你们能帮帮我吗?
谢谢你的
doInBackground
方法你怎么知道你在“法定规模”之内
你的整个方法建立在错误的/硬编码的假设上…记住数组的第一个元素是索引0。因此,长度将比最高索引高1,因此在使用它之前,需要从
randomLookupId
中减去1
(考虑长度为1的数组,您应该始终随机选择索引0)change
randomookupid=new Random().nextInt(jsonArray.length())
to randomLookupId=new Random().nextInt(jsonArray.length()-1);你能把代码中出现错误的那一行标出来吗?这将使事情变得更容易。但很可能你的ArrayGene是空的。在尝试访问其中的项目之前,您可以验证它的大小吗?ArrayGene始终是填充的,因为我根据用户选择的类型传递了int[]的其他初始化数组的值。编辑了我的问题,但即使更改返回给randomLookupId的值也无法使它工作!你能解释得更清楚些吗?无论如何,我还没有发布其他类,但我知道大小,因为我根据数组的长度选择了一个随机值!JSONObject returnObj=finalJsonArray.getJSONObject(0);如何确保finalJsonArray中有1个对象?还有,为什么要将名称final添加到变量中,而不将它们设置为final,如“final JSONObject finalJSONObject=new…”?我肯定有一个对象,因为我检索了一个随机的歌曲ID,上面有链接。因此它将始终处于位置0。至于“最后”一件事,这只是一个语言学问题,我不知道如何命名……创造力的丧失;)你为什么不调试一下,看看它在哪一行崩溃?我试过了,但它只是在PlayStream方法上崩溃了,那就是获取previewUrl并播放它…但是当previewUrl为null时,出现了异常!好吧,就像@thepoosh在评论中说的。但是现在logcat给了我另一个错误:randomLookupId=new Random().nextInt(jsonArray.length()-1);非法参数异常randomLookupId=new Random().nextInt((jsonArray.length()-1))代码>
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();
}