如何在 if 条件的末尾 Return 数组列表的对象?
How To Return Object of Array List at the end of if condition?
我有一个数组列表,它需要在末尾 return allteams 对象,但我无法做到这一点,当传递 null 时,它给出了一个空指针异常,请帮助我解决这个问题。这是代码示例:
private ArrayList<Team> getData() {
Intent localIntent = getIntent();
localIntent.getIntExtra("det", 0);
if (localIntent.hasExtra("beg")) {
Team t1 = new Team("Day 1: Chest, Back, Shoulders, Legs, Biceps, Triceps");
t1.players.add("Chest – Barbell Bench Press – 4 sets of 8 reps");
t1.players.add("Back – Lat-pulldowns – 4 sets of 10 reps");
t1.players.add("Shoulders – Seated Dumbbell Press – 4 sets of 10 reps");
t1.players.add("Legs – Leg Extensions – 4 sets of 10 reps");
t1.players.add("Biceps – Barbell Bbicep Curls – 3 sets of 10 reps");
t1.players.add("Triceps – Triceps Rope Pushdowns – 3 sets of 15 reps");
Team t2 = new Team("Day 2: Legs, Triceps, Biceps, Chest, Back, Shoulder");
t2.players.add("Legs – Leg Press Machine – 4 sets of 8 reps");
t2.players.add("Triceps – Overhead Bar Extensions – 3 sets of 20 reps");
t2.players.add("Biceps – EZ Bar Curls – 4 sets of 10 reps");
t2.players.add("Chest – Machine Chest Press – 4 sets of 10 reps");
t2.players.add("Back – T-Bar Row – 4 sets of 10 reps");
t2.players.add("Shoulders – Lateral Raises – 3 sets of 20 reps");
Team t3 = new Team("Day 3: Shoulders, Back, Chest, Legs, Triceps, Biceps");
t3.players.add("Shoulders – Upright Rows – 3 sets of 15 reps");
t3.players.add("Back – Close-Grip Pulldowns – 4 sets of 12 reps");
t3.players.add("Chest – Fly – 4 sets of 10 reps");
t3.players.add("Legs – Lunges – 3 sets of 10 reps per leg");
t3.players.add("Triceps – Skullcrushers – 3 sets of 15 reps");
t3.players.add("Biceps – Hammer Curls – 3 sets of 12 reps");
ArrayList<Team> allTeams = new ArrayList<Team>();
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
return allTeams;
} else if (localIntent.hasExtra("inter")) {
Team t1 = new Team("Day 1: Chest, Shoulders and Triceps");
t1.players.add("Bench Press – 3 sets of 10, 10, 8 (adding weight) reps");
t1.players.add("Incline Dumbbell Bench Press – 3 sets of 10 reps");
t1.players.add("Chest Dip – 3 sets of MAX reps");
t1.players.add("Skullcrushers – 3 sets of 8-10 Reps");
t1.players.add("One Arm Dumbbell Extension – 3 sets of 10 reps");
t1.players.add("Tricep Extension – 3 sets of 10 reps");
t1.players.add("Barbell Front Raise – 4 sets of 12 reps");
t1.players.add("Dumbbell Lateral Raise – 4 sets of 15, 12, 8, 8 (adding weight) reps");
Team t2 = new Team("Day 2: Back and Biceps");
t2.players.add("Wide Grip Pull Up 3 sets of MAX reps");
t2.players.add("Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Straight Arm Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Standing Barbell Curl – 3 sets of 8-10 reps");
t2.players.add("Preacher Curl – 3 sets of 10 reps");
t2.players.add("Incline Dumbbell Curl – 3 sets of 10 reps");
Team t3 = new Team("Day 3: Legs");
t3.players.add("Squat – 4 sets of 10,10,8,8 reps");
t3.players.add("Dumbbell Lunge – 3 sets of 8 on each leg");
t3.players.add("Leg Press – 3 sets of 12 reps");
t3.players.add("Leg Curl – 3 sets of 15 reps");
t3.players.add("Leg Extension – 3 sets of 15 reps");
t3.players.add("Standing Calf Raise – 5 sets of 10,8,8,8,6 (heavy)reps");
t3.players.add("Seated Calf Raise – 5 sets of 15 (light) reps");
Team t4 = new Team("Day 4: Shoulders, chest, and Triceps");
t4.players.add("Barbell Bench Press – 3 sets of 10, 10, 8 reps");
t4.players.add("Dumbbell Flys – 3 sets of 10 reps");
t4.players.add("Cable Crossovers – 3 sets of 10 reps");
t4.players.add("Close Grip Bench Press – 4 sets of 10, 10, 8, 6 reps");
t4.players.add("Lying Dumbbell Extension – 3 sets of 10 reps");
t4.players.add("Tricep Kickback – 3 sets of 10 reps");
t4.players.add("Seated Dumbbell Press – 4 sets of 10, 10, 8, 8 reps");
t4.players.add("One Arm Cable Lateral Raise – 3 sets of 12 reps");
Team t5 = new Team("Day 5: Back and Biceps");
t5.players.add("Seated Row – 4 sets of 10 reps");
t5.players.add("Bent Over Barbell Row – 3 sets of 10 reps");
t5.players.add("Bent Over Row – 3 sets of 12 reps");
t5.players.add("Smith Machine Upright Row – 3 sets of 8-10 reps");
t5.players.add("Cable Curl – 4 sets of 8-10 reps");
t5.players.add("Concentration Curl – 3 sets of 10 reps");
t5.players.add("Reverse Barbell Curl – 3 sets of 10 reps");
ArrayList<Team> allTeams = new ArrayList<Team>();
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
allTeams.add(t4);
allTeams.add(t5);
return allTeams;
}
return null;
}
请帮我解决这个问题我该怎么做才能在最后解决这个问题而不是 null 它应该是 returning allteams。
通过这些代码,如果不匹配任何东西,对于这些情况,它的 return null 添加最后一个 else 并设置它的默认值。无论它是否为 null 或不匹配,它都执行 else 语句。
if()
{
}
else if()
{
}
else {
Team t1 = new Team("Day 1: Chest, Shoulders and Triceps");
t1.players.add("Bench Press – 3 sets of 10, 10, 8 (adding weight) reps");
t1.players.add("Incline Dumbbell Bench Press – 3 sets of 10 reps");
t1.players.add("Chest Dip – 3 sets of MAX reps");
t1.players.add("Skullcrushers – 3 sets of 8-10 Reps");
t1.players.add("One Arm Dumbbell Extension – 3 sets of 10 reps");
t1.players.add("Tricep Extension – 3 sets of 10 reps");
t1.players.add("Barbell Front Raise – 4 sets of 12 reps");
t1.players.add("Dumbbell Lateral Raise – 4 sets of 15, 12, 8, 8 (adding weight) reps");
ArrayList<Team> allTeams = new ArrayList<Team>();
allTeams.add(t1);
return allTeams;
}
而你想要 return 一个数组,如果没有对应的条件?
因为你最后是returning null,如果没有对应的,你可以试试这个。
private ArrayList<Team> getData() {
Intent localIntent = getIntent();
localIntent.getIntExtra("det", 0);
ArrayList<Team> allTeams = new ArrayList<Team>();
if (localIntent.hasExtra("beg")) {
Team t1 = new Team("Day 1: Chest, Back, Shoulders, Legs, Biceps, Triceps");
t1.players.add("Chest – Barbell Bench Press – 4 sets of 8 reps");
t1.players.add("Back – Lat-pulldowns – 4 sets of 10 reps");
t1.players.add("Shoulders – Seated Dumbbell Press – 4 sets of 10 reps");
t1.players.add("Legs – Leg Extensions – 4 sets of 10 reps");
t1.players.add("Biceps – Barbell Bbicep Curls – 3 sets of 10 reps");
t1.players.add("Triceps – Triceps Rope Pushdowns – 3 sets of 15 reps");
Team t2 = new Team("Day 2: Legs, Triceps, Biceps, Chest, Back, Shoulder");
t2.players.add("Legs – Leg Press Machine – 4 sets of 8 reps");
t2.players.add("Triceps – Overhead Bar Extensions – 3 sets of 20 reps");
t2.players.add("Biceps – EZ Bar Curls – 4 sets of 10 reps");
t2.players.add("Chest – Machine Chest Press – 4 sets of 10 reps");
t2.players.add("Back – T-Bar Row – 4 sets of 10 reps");
t2.players.add("Shoulders – Lateral Raises – 3 sets of 20 reps");
Team t3 = new Team("Day 3: Shoulders, Back, Chest, Legs, Triceps, Biceps");
t3.players.add("Shoulders – Upright Rows – 3 sets of 15 reps");
t3.players.add("Back – Close-Grip Pulldowns – 4 sets of 12 reps");
t3.players.add("Chest – Fly – 4 sets of 10 reps");
t3.players.add("Legs – Lunges – 3 sets of 10 reps per leg");
t3.players.add("Triceps – Skullcrushers – 3 sets of 15 reps");
t3.players.add("Biceps – Hammer Curls – 3 sets of 12 reps");
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
return allTeams;
} else if (localIntent.hasExtra("inter")) {
Team t1 = new Team("Day 1: Chest, Shoulders and Triceps");
t1.players.add("Bench Press – 3 sets of 10, 10, 8 (adding weight) reps");
t1.players.add("Incline Dumbbell Bench Press – 3 sets of 10 reps");
t1.players.add("Chest Dip – 3 sets of MAX reps");
t1.players.add("Skullcrushers – 3 sets of 8-10 Reps");
t1.players.add("One Arm Dumbbell Extension – 3 sets of 10 reps");
t1.players.add("Tricep Extension – 3 sets of 10 reps");
t1.players.add("Barbell Front Raise – 4 sets of 12 reps");
t1.players.add("Dumbbell Lateral Raise – 4 sets of 15, 12, 8, 8 (adding weight) reps");
Team t2 = new Team("Day 2: Back and Biceps");
t2.players.add("Wide Grip Pull Up 3 sets of MAX reps");
t2.players.add("Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Straight Arm Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Standing Barbell Curl – 3 sets of 8-10 reps");
t2.players.add("Preacher Curl – 3 sets of 10 reps");
t2.players.add("Incline Dumbbell Curl – 3 sets of 10 reps");
Team t3 = new Team("Day 3: Legs");
t3.players.add("Squat – 4 sets of 10,10,8,8 reps");
t3.players.add("Dumbbell Lunge – 3 sets of 8 on each leg");
t3.players.add("Leg Press – 3 sets of 12 reps");
t3.players.add("Leg Curl – 3 sets of 15 reps");
t3.players.add("Leg Extension – 3 sets of 15 reps");
t3.players.add("Standing Calf Raise – 5 sets of 10,8,8,8,6 (heavy)reps");
t3.players.add("Seated Calf Raise – 5 sets of 15 (light) reps");
Team t4 = new Team("Day 4: Shoulders, chest, and Triceps");
t4.players.add("Barbell Bench Press – 3 sets of 10, 10, 8 reps");
t4.players.add("Dumbbell Flys – 3 sets of 10 reps");
t4.players.add("Cable Crossovers – 3 sets of 10 reps");
t4.players.add("Close Grip Bench Press – 4 sets of 10, 10, 8, 6 reps");
t4.players.add("Lying Dumbbell Extension – 3 sets of 10 reps");
t4.players.add("Tricep Kickback – 3 sets of 10 reps");
t4.players.add("Seated Dumbbell Press – 4 sets of 10, 10, 8, 8 reps");
t4.players.add("One Arm Cable Lateral Raise – 3 sets of 12 reps");
Team t5 = new Team("Day 5: Back and Biceps");
t5.players.add("Seated Row – 4 sets of 10 reps");
t5.players.add("Bent Over Barbell Row – 3 sets of 10 reps");
t5.players.add("Bent Over Row – 3 sets of 12 reps");
t5.players.add("Smith Machine Upright Row – 3 sets of 8-10 reps");
t5.players.add("Cable Curl – 4 sets of 8-10 reps");
t5.players.add("Concentration Curl – 3 sets of 10 reps");
t5.players.add("Reverse Barbell Curl – 3 sets of 10 reps");
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
allTeams.add(t4);
allTeams.add(t5);
return allTeams;
}
return allTeams;
}
我已将 allTeams 数组放在方法的开头。你可以试试看是否有效。但是如果真的要return null 的时候没有条件。但是您需要检查 returning 中的 allTeams 是否不为空。
private ArrayList<Team> getData() {
Intent localIntent = getIntent();
localIntent.getIntExtra("det", 0);
ArrayList<Team> allTeams = new ArrayList<Team>();
if (localIntent.hasExtra("beg")) {
Team t1 = new Team("Day 1: Chest, Back, Shoulders, Legs, Biceps, Triceps");
t1.players.add("Chest – Barbell Bench Press – 4 sets of 8 reps");
t1.players.add("Back – Lat-pulldowns – 4 sets of 10 reps");
t1.players.add("Shoulders – Seated Dumbbell Press – 4 sets of 10 reps");
t1.players.add("Legs – Leg Extensions – 4 sets of 10 reps");
t1.players.add("Biceps – Barbell Bbicep Curls – 3 sets of 10 reps");
t1.players.add("Triceps – Triceps Rope Pushdowns – 3 sets of 15 reps");
Team t2 = new Team("Day 2: Legs, Triceps, Biceps, Chest, Back, Shoulder");
t2.players.add("Legs – Leg Press Machine – 4 sets of 8 reps");
t2.players.add("Triceps – Overhead Bar Extensions – 3 sets of 20 reps");
t2.players.add("Biceps – EZ Bar Curls – 4 sets of 10 reps");
t2.players.add("Chest – Machine Chest Press – 4 sets of 10 reps");
t2.players.add("Back – T-Bar Row – 4 sets of 10 reps");
t2.players.add("Shoulders – Lateral Raises – 3 sets of 20 reps");
Team t3 = new Team("Day 3: Shoulders, Back, Chest, Legs, Triceps, Biceps");
t3.players.add("Shoulders – Upright Rows – 3 sets of 15 reps");
t3.players.add("Back – Close-Grip Pulldowns – 4 sets of 12 reps");
t3.players.add("Chest – Fly – 4 sets of 10 reps");
t3.players.add("Legs – Lunges – 3 sets of 10 reps per leg");
t3.players.add("Triceps – Skullcrushers – 3 sets of 15 reps");
t3.players.add("Biceps – Hammer Curls – 3 sets of 12 reps");
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
} else if (localIntent.hasExtra("inter")) {
Team t1 = new Team("Day 1: Chest, Shoulders and Triceps");
t1.players.add("Bench Press – 3 sets of 10, 10, 8 (adding weight) reps");
t1.players.add("Incline Dumbbell Bench Press – 3 sets of 10 reps");
t1.players.add("Chest Dip – 3 sets of MAX reps");
t1.players.add("Skullcrushers – 3 sets of 8-10 Reps");
t1.players.add("One Arm Dumbbell Extension – 3 sets of 10 reps");
t1.players.add("Tricep Extension – 3 sets of 10 reps");
t1.players.add("Barbell Front Raise – 4 sets of 12 reps");
t1.players.add("Dumbbell Lateral Raise – 4 sets of 15, 12, 8, 8 (adding weight) reps");
Team t2 = new Team("Day 2: Back and Biceps");
t2.players.add("Wide Grip Pull Up 3 sets of MAX reps");
t2.players.add("Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Straight Arm Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Standing Barbell Curl – 3 sets of 8-10 reps");
t2.players.add("Preacher Curl – 3 sets of 10 reps");
t2.players.add("Incline Dumbbell Curl – 3 sets of 10 reps");
Team t3 = new Team("Day 3: Legs");
t3.players.add("Squat – 4 sets of 10,10,8,8 reps");
t3.players.add("Dumbbell Lunge – 3 sets of 8 on each leg");
t3.players.add("Leg Press – 3 sets of 12 reps");
t3.players.add("Leg Curl – 3 sets of 15 reps");
t3.players.add("Leg Extension – 3 sets of 15 reps");
t3.players.add("Standing Calf Raise – 5 sets of 10,8,8,8,6 (heavy)reps");
t3.players.add("Seated Calf Raise – 5 sets of 15 (light) reps");
Team t4 = new Team("Day 4: Shoulders, chest, and Triceps");
t4.players.add("Barbell Bench Press – 3 sets of 10, 10, 8 reps");
t4.players.add("Dumbbell Flys – 3 sets of 10 reps");
t4.players.add("Cable Crossovers – 3 sets of 10 reps");
t4.players.add("Close Grip Bench Press – 4 sets of 10, 10, 8, 6 reps");
t4.players.add("Lying Dumbbell Extension – 3 sets of 10 reps");
t4.players.add("Tricep Kickback – 3 sets of 10 reps");
t4.players.add("Seated Dumbbell Press – 4 sets of 10, 10, 8, 8 reps");
t4.players.add("One Arm Cable Lateral Raise – 3 sets of 12 reps");
Team t5 = new Team("Day 5: Back and Biceps");
t5.players.add("Seated Row – 4 sets of 10 reps");
t5.players.add("Bent Over Barbell Row – 3 sets of 10 reps");
t5.players.add("Bent Over Row – 3 sets of 12 reps");
t5.players.add("Smith Machine Upright Row – 3 sets of 8-10 reps");
t5.players.add("Cable Curl – 4 sets of 8-10 reps");
t5.players.add("Concentration Curl – 3 sets of 10 reps");
t5.players.add("Reverse Barbell Curl – 3 sets of 10 reps");
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
allTeams.add(t4);
allTeams.add(t5);
}
return allTeams;
}
试试这个,无论你在哪里使用这个方法,在使用之前检查一下
ArrayList teamList = getData()
if(teamList.isNotEmpty()){
*do your stuff with teamList here*
}
我有一个数组列表,它需要在末尾 return allteams 对象,但我无法做到这一点,当传递 null 时,它给出了一个空指针异常,请帮助我解决这个问题。这是代码示例:
private ArrayList<Team> getData() {
Intent localIntent = getIntent();
localIntent.getIntExtra("det", 0);
if (localIntent.hasExtra("beg")) {
Team t1 = new Team("Day 1: Chest, Back, Shoulders, Legs, Biceps, Triceps");
t1.players.add("Chest – Barbell Bench Press – 4 sets of 8 reps");
t1.players.add("Back – Lat-pulldowns – 4 sets of 10 reps");
t1.players.add("Shoulders – Seated Dumbbell Press – 4 sets of 10 reps");
t1.players.add("Legs – Leg Extensions – 4 sets of 10 reps");
t1.players.add("Biceps – Barbell Bbicep Curls – 3 sets of 10 reps");
t1.players.add("Triceps – Triceps Rope Pushdowns – 3 sets of 15 reps");
Team t2 = new Team("Day 2: Legs, Triceps, Biceps, Chest, Back, Shoulder");
t2.players.add("Legs – Leg Press Machine – 4 sets of 8 reps");
t2.players.add("Triceps – Overhead Bar Extensions – 3 sets of 20 reps");
t2.players.add("Biceps – EZ Bar Curls – 4 sets of 10 reps");
t2.players.add("Chest – Machine Chest Press – 4 sets of 10 reps");
t2.players.add("Back – T-Bar Row – 4 sets of 10 reps");
t2.players.add("Shoulders – Lateral Raises – 3 sets of 20 reps");
Team t3 = new Team("Day 3: Shoulders, Back, Chest, Legs, Triceps, Biceps");
t3.players.add("Shoulders – Upright Rows – 3 sets of 15 reps");
t3.players.add("Back – Close-Grip Pulldowns – 4 sets of 12 reps");
t3.players.add("Chest – Fly – 4 sets of 10 reps");
t3.players.add("Legs – Lunges – 3 sets of 10 reps per leg");
t3.players.add("Triceps – Skullcrushers – 3 sets of 15 reps");
t3.players.add("Biceps – Hammer Curls – 3 sets of 12 reps");
ArrayList<Team> allTeams = new ArrayList<Team>();
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
return allTeams;
} else if (localIntent.hasExtra("inter")) {
Team t1 = new Team("Day 1: Chest, Shoulders and Triceps");
t1.players.add("Bench Press – 3 sets of 10, 10, 8 (adding weight) reps");
t1.players.add("Incline Dumbbell Bench Press – 3 sets of 10 reps");
t1.players.add("Chest Dip – 3 sets of MAX reps");
t1.players.add("Skullcrushers – 3 sets of 8-10 Reps");
t1.players.add("One Arm Dumbbell Extension – 3 sets of 10 reps");
t1.players.add("Tricep Extension – 3 sets of 10 reps");
t1.players.add("Barbell Front Raise – 4 sets of 12 reps");
t1.players.add("Dumbbell Lateral Raise – 4 sets of 15, 12, 8, 8 (adding weight) reps");
Team t2 = new Team("Day 2: Back and Biceps");
t2.players.add("Wide Grip Pull Up 3 sets of MAX reps");
t2.players.add("Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Straight Arm Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Standing Barbell Curl – 3 sets of 8-10 reps");
t2.players.add("Preacher Curl – 3 sets of 10 reps");
t2.players.add("Incline Dumbbell Curl – 3 sets of 10 reps");
Team t3 = new Team("Day 3: Legs");
t3.players.add("Squat – 4 sets of 10,10,8,8 reps");
t3.players.add("Dumbbell Lunge – 3 sets of 8 on each leg");
t3.players.add("Leg Press – 3 sets of 12 reps");
t3.players.add("Leg Curl – 3 sets of 15 reps");
t3.players.add("Leg Extension – 3 sets of 15 reps");
t3.players.add("Standing Calf Raise – 5 sets of 10,8,8,8,6 (heavy)reps");
t3.players.add("Seated Calf Raise – 5 sets of 15 (light) reps");
Team t4 = new Team("Day 4: Shoulders, chest, and Triceps");
t4.players.add("Barbell Bench Press – 3 sets of 10, 10, 8 reps");
t4.players.add("Dumbbell Flys – 3 sets of 10 reps");
t4.players.add("Cable Crossovers – 3 sets of 10 reps");
t4.players.add("Close Grip Bench Press – 4 sets of 10, 10, 8, 6 reps");
t4.players.add("Lying Dumbbell Extension – 3 sets of 10 reps");
t4.players.add("Tricep Kickback – 3 sets of 10 reps");
t4.players.add("Seated Dumbbell Press – 4 sets of 10, 10, 8, 8 reps");
t4.players.add("One Arm Cable Lateral Raise – 3 sets of 12 reps");
Team t5 = new Team("Day 5: Back and Biceps");
t5.players.add("Seated Row – 4 sets of 10 reps");
t5.players.add("Bent Over Barbell Row – 3 sets of 10 reps");
t5.players.add("Bent Over Row – 3 sets of 12 reps");
t5.players.add("Smith Machine Upright Row – 3 sets of 8-10 reps");
t5.players.add("Cable Curl – 4 sets of 8-10 reps");
t5.players.add("Concentration Curl – 3 sets of 10 reps");
t5.players.add("Reverse Barbell Curl – 3 sets of 10 reps");
ArrayList<Team> allTeams = new ArrayList<Team>();
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
allTeams.add(t4);
allTeams.add(t5);
return allTeams;
}
return null;
}
请帮我解决这个问题我该怎么做才能在最后解决这个问题而不是 null 它应该是 returning allteams。
通过这些代码,如果不匹配任何东西,对于这些情况,它的 return null 添加最后一个 else 并设置它的默认值。无论它是否为 null 或不匹配,它都执行 else 语句。
if()
{
}
else if()
{
}
else {
Team t1 = new Team("Day 1: Chest, Shoulders and Triceps");
t1.players.add("Bench Press – 3 sets of 10, 10, 8 (adding weight) reps");
t1.players.add("Incline Dumbbell Bench Press – 3 sets of 10 reps");
t1.players.add("Chest Dip – 3 sets of MAX reps");
t1.players.add("Skullcrushers – 3 sets of 8-10 Reps");
t1.players.add("One Arm Dumbbell Extension – 3 sets of 10 reps");
t1.players.add("Tricep Extension – 3 sets of 10 reps");
t1.players.add("Barbell Front Raise – 4 sets of 12 reps");
t1.players.add("Dumbbell Lateral Raise – 4 sets of 15, 12, 8, 8 (adding weight) reps");
ArrayList<Team> allTeams = new ArrayList<Team>();
allTeams.add(t1);
return allTeams;
}
而你想要 return 一个数组,如果没有对应的条件? 因为你最后是returning null,如果没有对应的,你可以试试这个。
private ArrayList<Team> getData() {
Intent localIntent = getIntent();
localIntent.getIntExtra("det", 0);
ArrayList<Team> allTeams = new ArrayList<Team>();
if (localIntent.hasExtra("beg")) {
Team t1 = new Team("Day 1: Chest, Back, Shoulders, Legs, Biceps, Triceps");
t1.players.add("Chest – Barbell Bench Press – 4 sets of 8 reps");
t1.players.add("Back – Lat-pulldowns – 4 sets of 10 reps");
t1.players.add("Shoulders – Seated Dumbbell Press – 4 sets of 10 reps");
t1.players.add("Legs – Leg Extensions – 4 sets of 10 reps");
t1.players.add("Biceps – Barbell Bbicep Curls – 3 sets of 10 reps");
t1.players.add("Triceps – Triceps Rope Pushdowns – 3 sets of 15 reps");
Team t2 = new Team("Day 2: Legs, Triceps, Biceps, Chest, Back, Shoulder");
t2.players.add("Legs – Leg Press Machine – 4 sets of 8 reps");
t2.players.add("Triceps – Overhead Bar Extensions – 3 sets of 20 reps");
t2.players.add("Biceps – EZ Bar Curls – 4 sets of 10 reps");
t2.players.add("Chest – Machine Chest Press – 4 sets of 10 reps");
t2.players.add("Back – T-Bar Row – 4 sets of 10 reps");
t2.players.add("Shoulders – Lateral Raises – 3 sets of 20 reps");
Team t3 = new Team("Day 3: Shoulders, Back, Chest, Legs, Triceps, Biceps");
t3.players.add("Shoulders – Upright Rows – 3 sets of 15 reps");
t3.players.add("Back – Close-Grip Pulldowns – 4 sets of 12 reps");
t3.players.add("Chest – Fly – 4 sets of 10 reps");
t3.players.add("Legs – Lunges – 3 sets of 10 reps per leg");
t3.players.add("Triceps – Skullcrushers – 3 sets of 15 reps");
t3.players.add("Biceps – Hammer Curls – 3 sets of 12 reps");
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
return allTeams;
} else if (localIntent.hasExtra("inter")) {
Team t1 = new Team("Day 1: Chest, Shoulders and Triceps");
t1.players.add("Bench Press – 3 sets of 10, 10, 8 (adding weight) reps");
t1.players.add("Incline Dumbbell Bench Press – 3 sets of 10 reps");
t1.players.add("Chest Dip – 3 sets of MAX reps");
t1.players.add("Skullcrushers – 3 sets of 8-10 Reps");
t1.players.add("One Arm Dumbbell Extension – 3 sets of 10 reps");
t1.players.add("Tricep Extension – 3 sets of 10 reps");
t1.players.add("Barbell Front Raise – 4 sets of 12 reps");
t1.players.add("Dumbbell Lateral Raise – 4 sets of 15, 12, 8, 8 (adding weight) reps");
Team t2 = new Team("Day 2: Back and Biceps");
t2.players.add("Wide Grip Pull Up 3 sets of MAX reps");
t2.players.add("Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Straight Arm Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Standing Barbell Curl – 3 sets of 8-10 reps");
t2.players.add("Preacher Curl – 3 sets of 10 reps");
t2.players.add("Incline Dumbbell Curl – 3 sets of 10 reps");
Team t3 = new Team("Day 3: Legs");
t3.players.add("Squat – 4 sets of 10,10,8,8 reps");
t3.players.add("Dumbbell Lunge – 3 sets of 8 on each leg");
t3.players.add("Leg Press – 3 sets of 12 reps");
t3.players.add("Leg Curl – 3 sets of 15 reps");
t3.players.add("Leg Extension – 3 sets of 15 reps");
t3.players.add("Standing Calf Raise – 5 sets of 10,8,8,8,6 (heavy)reps");
t3.players.add("Seated Calf Raise – 5 sets of 15 (light) reps");
Team t4 = new Team("Day 4: Shoulders, chest, and Triceps");
t4.players.add("Barbell Bench Press – 3 sets of 10, 10, 8 reps");
t4.players.add("Dumbbell Flys – 3 sets of 10 reps");
t4.players.add("Cable Crossovers – 3 sets of 10 reps");
t4.players.add("Close Grip Bench Press – 4 sets of 10, 10, 8, 6 reps");
t4.players.add("Lying Dumbbell Extension – 3 sets of 10 reps");
t4.players.add("Tricep Kickback – 3 sets of 10 reps");
t4.players.add("Seated Dumbbell Press – 4 sets of 10, 10, 8, 8 reps");
t4.players.add("One Arm Cable Lateral Raise – 3 sets of 12 reps");
Team t5 = new Team("Day 5: Back and Biceps");
t5.players.add("Seated Row – 4 sets of 10 reps");
t5.players.add("Bent Over Barbell Row – 3 sets of 10 reps");
t5.players.add("Bent Over Row – 3 sets of 12 reps");
t5.players.add("Smith Machine Upright Row – 3 sets of 8-10 reps");
t5.players.add("Cable Curl – 4 sets of 8-10 reps");
t5.players.add("Concentration Curl – 3 sets of 10 reps");
t5.players.add("Reverse Barbell Curl – 3 sets of 10 reps");
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
allTeams.add(t4);
allTeams.add(t5);
return allTeams;
}
return allTeams;
}
我已将 allTeams 数组放在方法的开头。你可以试试看是否有效。但是如果真的要return null 的时候没有条件。但是您需要检查 returning 中的 allTeams 是否不为空。
private ArrayList<Team> getData() {
Intent localIntent = getIntent();
localIntent.getIntExtra("det", 0);
ArrayList<Team> allTeams = new ArrayList<Team>();
if (localIntent.hasExtra("beg")) {
Team t1 = new Team("Day 1: Chest, Back, Shoulders, Legs, Biceps, Triceps");
t1.players.add("Chest – Barbell Bench Press – 4 sets of 8 reps");
t1.players.add("Back – Lat-pulldowns – 4 sets of 10 reps");
t1.players.add("Shoulders – Seated Dumbbell Press – 4 sets of 10 reps");
t1.players.add("Legs – Leg Extensions – 4 sets of 10 reps");
t1.players.add("Biceps – Barbell Bbicep Curls – 3 sets of 10 reps");
t1.players.add("Triceps – Triceps Rope Pushdowns – 3 sets of 15 reps");
Team t2 = new Team("Day 2: Legs, Triceps, Biceps, Chest, Back, Shoulder");
t2.players.add("Legs – Leg Press Machine – 4 sets of 8 reps");
t2.players.add("Triceps – Overhead Bar Extensions – 3 sets of 20 reps");
t2.players.add("Biceps – EZ Bar Curls – 4 sets of 10 reps");
t2.players.add("Chest – Machine Chest Press – 4 sets of 10 reps");
t2.players.add("Back – T-Bar Row – 4 sets of 10 reps");
t2.players.add("Shoulders – Lateral Raises – 3 sets of 20 reps");
Team t3 = new Team("Day 3: Shoulders, Back, Chest, Legs, Triceps, Biceps");
t3.players.add("Shoulders – Upright Rows – 3 sets of 15 reps");
t3.players.add("Back – Close-Grip Pulldowns – 4 sets of 12 reps");
t3.players.add("Chest – Fly – 4 sets of 10 reps");
t3.players.add("Legs – Lunges – 3 sets of 10 reps per leg");
t3.players.add("Triceps – Skullcrushers – 3 sets of 15 reps");
t3.players.add("Biceps – Hammer Curls – 3 sets of 12 reps");
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
} else if (localIntent.hasExtra("inter")) {
Team t1 = new Team("Day 1: Chest, Shoulders and Triceps");
t1.players.add("Bench Press – 3 sets of 10, 10, 8 (adding weight) reps");
t1.players.add("Incline Dumbbell Bench Press – 3 sets of 10 reps");
t1.players.add("Chest Dip – 3 sets of MAX reps");
t1.players.add("Skullcrushers – 3 sets of 8-10 Reps");
t1.players.add("One Arm Dumbbell Extension – 3 sets of 10 reps");
t1.players.add("Tricep Extension – 3 sets of 10 reps");
t1.players.add("Barbell Front Raise – 4 sets of 12 reps");
t1.players.add("Dumbbell Lateral Raise – 4 sets of 15, 12, 8, 8 (adding weight) reps");
Team t2 = new Team("Day 2: Back and Biceps");
t2.players.add("Wide Grip Pull Up 3 sets of MAX reps");
t2.players.add("Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Straight Arm Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Standing Barbell Curl – 3 sets of 8-10 reps");
t2.players.add("Preacher Curl – 3 sets of 10 reps");
t2.players.add("Incline Dumbbell Curl – 3 sets of 10 reps");
Team t3 = new Team("Day 3: Legs");
t3.players.add("Squat – 4 sets of 10,10,8,8 reps");
t3.players.add("Dumbbell Lunge – 3 sets of 8 on each leg");
t3.players.add("Leg Press – 3 sets of 12 reps");
t3.players.add("Leg Curl – 3 sets of 15 reps");
t3.players.add("Leg Extension – 3 sets of 15 reps");
t3.players.add("Standing Calf Raise – 5 sets of 10,8,8,8,6 (heavy)reps");
t3.players.add("Seated Calf Raise – 5 sets of 15 (light) reps");
Team t4 = new Team("Day 4: Shoulders, chest, and Triceps");
t4.players.add("Barbell Bench Press – 3 sets of 10, 10, 8 reps");
t4.players.add("Dumbbell Flys – 3 sets of 10 reps");
t4.players.add("Cable Crossovers – 3 sets of 10 reps");
t4.players.add("Close Grip Bench Press – 4 sets of 10, 10, 8, 6 reps");
t4.players.add("Lying Dumbbell Extension – 3 sets of 10 reps");
t4.players.add("Tricep Kickback – 3 sets of 10 reps");
t4.players.add("Seated Dumbbell Press – 4 sets of 10, 10, 8, 8 reps");
t4.players.add("One Arm Cable Lateral Raise – 3 sets of 12 reps");
Team t5 = new Team("Day 5: Back and Biceps");
t5.players.add("Seated Row – 4 sets of 10 reps");
t5.players.add("Bent Over Barbell Row – 3 sets of 10 reps");
t5.players.add("Bent Over Row – 3 sets of 12 reps");
t5.players.add("Smith Machine Upright Row – 3 sets of 8-10 reps");
t5.players.add("Cable Curl – 4 sets of 8-10 reps");
t5.players.add("Concentration Curl – 3 sets of 10 reps");
t5.players.add("Reverse Barbell Curl – 3 sets of 10 reps");
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
allTeams.add(t4);
allTeams.add(t5);
}
return allTeams;
}
试试这个,无论你在哪里使用这个方法,在使用之前检查一下
ArrayList teamList = getData()
if(teamList.isNotEmpty()){
*do your stuff with teamList here*
}