英文:
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<Categories> displayDataaaa(List<Categories> categoriesList) {
this.categoriesList = new ArrayList<Categories>();
/
String cat_name = "";
// this.categoriesList.add(new Categories(1,"lkajsdflk"));
this.categoriesList.add(new Categories(1, categoriesList.get(0).getCategoriesName()));
for (int i = 0; i < categoriesList.size(); i++) {
cat_name = categoriesList.get(i).getCategoriesName();
// this.categoriesList.add(new Categories(i, cat_name));
Log.d("displayDataaaaa-cat_name", "displayDataaaaa-cat_name" + 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) 
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) 
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论