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

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

Problem to return list which contains items from rest api

问题

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

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

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

public List<Categories> displayDataaaa(List<Categories> categoriesList) {

    this.categoriesList = new ArrayList<Categories>();
    // ...
    String cat_name = "";

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

    for (int i = 0; i < categoriesList.size(); i++) {
        cat_name = categoriesList.get(i).getCategoriesName();
        Log.d("displayDataaaaa-cat_name", "displayDataaaaa-cat_name" + cat_name);
        this.categoriesList.addAll(categoriesList);
    }

    return this.categoriesList;
}

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

英文:

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:

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

the method which should return items:

public List&lt;Categories&gt; displayDataaaa(List&lt;Categories&gt; categoriesList) {

    this.categoriesList = new ArrayList&lt;Categories&gt;();
    /
    String cat_name = &quot;&quot;;

    // this.categoriesList.add(new Categories(1,&quot;lkajsdflk&quot;));
    this.categoriesList.add(new Categories(1, categoriesList.get(0).getCategoriesName()));


    for (int i = 0; i &lt; categoriesList.size(); i++) {
         cat_name = categoriesList.get(i).getCategoriesName();
      //    this.categoriesList.add(new Categories(i, cat_name));
        Log.d(&quot;displayDataaaaa-cat_name&quot;, &quot;displayDataaaaa-cat_name&quot; + cat_name);

        this.categoriesList.addAll(categoriesList);
    }
 //   this.categoriesList.add(new Categories(1, cat_name));

    return this.categoriesList;
}

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

Log:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{dev4passion.partyj10/dev4passion.partyj10.MainActivity}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.get(ArrayList.java:437)
    at dev4passion.partyj10.MainActivity.displayDataaaa(MainActivity.java:189)
    at dev4passion.partyj10.MainActivity.onCreate(MainActivity.java:94)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)&#160;
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)&#160;
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)&#160;
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)&#160;
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)&#160;
    at android.os.Handler.dispatchMessage(Handler.java:106)&#160;
    at android.os.Looper.loop(Looper.java:193)&#160;
    at android.app.ActivityThread.main(ActivityThread.java:6669)&#160;
    at java.lang.reflect.Method.invoke(Native Method)&#160;
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)&#160;
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)&#160;

答案1

得分: 1

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

英文:
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:

确定