返回包含来自REST API的项目的列表。

huangapple go评论111阅读模式
英文:

Problem to return list which contains items from rest api

问题

我在Android中的一个方法中遇到了问题,该方法应该返回从REST API接收到的项目列表。我能够在该方法中调试这些项目,因为我在onCreate方法中编写了该方法的声明。但是,问题出现在我尝试将这些项目(在该方法中)添加到列表中,以便该方法可以返回它们时。我收到了以下错误消息:

  1. Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

应该返回项目的方法如下:

  1. public List<Categories> displayDataaaa(List<Categories> categoriesList) {
  2. this.categoriesList = new ArrayList<Categories>();
  3. // ...
  4. String cat_name = "";
  5. this.categoriesList.add(new Categories(1, categoriesList.get(0).getCategoriesName()));
  6. for (int i = 0; i < categoriesList.size(); i++) {
  7. cat_name = categoriesList.get(i).getCategoriesName();
  8. Log.d("displayDataaaaa-cat_name", "displayDataaaaa-cat_name" + cat_name);
  9. this.categoriesList.addAll(categoriesList);
  10. }
  11. return this.categoriesList;
  12. }

请问是否可以提供一些提示?谢谢。

英文:

I've got a problem with a method in Android which should return a list of items which are received from rest api. I am able to debug these items in that method - because I wrote declaration of this method in the onCreate method. But the problem occurs once I try to add these items (in that method) to the list, so the method could return these. I receive an error like:

  1. Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

the method which should return items:

  1. public List&lt;Categories&gt; displayDataaaa(List&lt;Categories&gt; categoriesList) {
  2. this.categoriesList = new ArrayList&lt;Categories&gt;();
  3. /
  4. String cat_name = &quot;&quot;;
  5. // this.categoriesList.add(new Categories(1,&quot;lkajsdflk&quot;));
  6. this.categoriesList.add(new Categories(1, categoriesList.get(0).getCategoriesName()));
  7. for (int i = 0; i &lt; categoriesList.size(); i++) {
  8. cat_name = categoriesList.get(i).getCategoriesName();
  9. // this.categoriesList.add(new Categories(i, cat_name));
  10. Log.d(&quot;displayDataaaaa-cat_name&quot;, &quot;displayDataaaaa-cat_name&quot; + cat_name);
  11. this.categoriesList.addAll(categoriesList);
  12. }
  13. // this.categoriesList.add(new Categories(1, cat_name));
  14. return this.categoriesList;
  15. }

Could I ask you for some hint, please?
Thank you.

Log:

  1. java.lang.RuntimeException: Unable to start activity ComponentInfo{dev4passion.partyj10/dev4passion.partyj10.MainActivity}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
  2. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
  3. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
  4. at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
  5. at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
  6. at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
  7. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
  8. at android.os.Handler.dispatchMessage(Handler.java:106)
  9. at android.os.Looper.loop(Looper.java:193)
  10. at android.app.ActivityThread.main(ActivityThread.java:6669)
  11. at java.lang.reflect.Method.invoke(Native Method)
  12. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
  13. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
  14. Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
  15. at java.util.ArrayList.get(ArrayList.java:437)
  16. at dev4passion.partyj10.MainActivity.displayDataaaa(MainActivity.java:189)
  17. at dev4passion.partyj10.MainActivity.onCreate(MainActivity.java:94)
  18. at android.app.Activity.performCreate(Activity.java:7136)
  19. at android.app.Activity.performCreate(Activity.java:7127)
  20. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
  21. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
  22. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)&#160;
  23. at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)&#160;
  24. at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)&#160;
  25. at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)&#160;
  26. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)&#160;
  27. at android.os.Handler.dispatchMessage(Handler.java:106)&#160;
  28. at android.os.Looper.loop(Looper.java:193)&#160;
  29. at android.app.ActivityThread.main(ActivityThread.java:6669)&#160;
  30. at java.lang.reflect.Method.invoke(Native Method)&#160;
  31. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)&#160;
  32. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)&#160;

答案1

得分: 1

这是您的问题行。当categoriesList为空时,categoriesList.get(0)会引发异常。

英文:
  1. this.categoriesList.add(new Categories(1, categoriesList.get(0).getCategoriesName()));

This is your problem line. categoriesList.get(0) will throw an exception when categoriesList is empty.

huangapple
  • 本文由 发表于 2020年8月9日 04:18:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63319868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定