由于某些原因我得到此错误,这里是错误异常的缩短版本。我厌倦了提供尽可能多的细节,但我真的不知道如何处理这个问题:(
09-16 19:39:18.404 15774-15774/com.spizer.mizer2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.spizer.mizer2, PID: 15774
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spizer.mizer2/com.spizer.mizer2.DifficultyMenu}: java.lang.IndexOutOfBoundsException: Invalid index 3, size is 2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2712)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2777)
at android.app.ActivityThread.access$900(ActivityThread.java:179)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 3, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.spizer.mizer2.DifficultyMenu.prepareListData(DifficultyMenu.java:187)
at com.spizer.mizer2.DifficultyMenu.onCreate(DifficultyMenu.java:43)
at android.app.Activity.performCreate(Activity.java:6295)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2777)
at android.app.ActivityThread.access$900(ActivityThread.java:179)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
这里是我的课程代码
package com.spizer.mizer2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.view.View;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
public class DifficultyMenu extends AppCompatActivity {
/** calls to make the class ProblemSelector usable in this class **/
ProblemSelector PS = new ProblemSelector();
private int T1;
private int T2;
private int T3;
private int T4;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_difficulty_menu);
// get the list view
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// List view Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// List view Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// List view Group collapsed listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// List view on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT)
.show();
/** converts the selected number in the expandable list to a usable integer variable **/
if(listDataHeader.get(groupPosition).equals("AdditionDifficulty")) {
String St1 = listDataHeader.get(childPosition);
int AddDiff = Integer.parseInt(St1);
}
else if(listDataHeader.get(groupPosition).equals("SubtractionDifficulty")) {
String St2 = listDataHeader.get(childPosition);
int SubDiff = Integer.parseInt(St2);
}
else if(listDataHeader.get(groupPosition).equals("MultiplicationDifficulty")) {
String St3 = listDataHeader.get(childPosition);
int MultiDIff = Integer.parseInt(St3);
}
else if(listDataHeader.get(groupPosition).equals("DivisionDifficulty")) {
String St4 = listDataHeader.get(childPosition);
int DivisDiff = Integer.parseInt(St4);
}
// else {
// /** public static int e("DifficultyMenu.class", "could not parse a group position when user selected a field"); **/
// }
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<>();
listDataChild = new HashMap<>();
// Adding child data
if(PS.AddProb) { listDataHeader.add("AdditionDifficulty"); }
if(PS.SubProb) { listDataHeader.add("SubtractionDifficulty"); }
if(PS.MultiProb) { listDataHeader.add("MultiplicationDifficulty"); }
if(PS.DivisProb) { listDataHeader.add("DivisionDifficulty"); }
/** this removes fields that the user has not selected to practice **/
if(!PS.AddProb) { listDataHeader.remove("AdditionDifficulty"); }
if(!PS.SubProb) { listDataHeader.remove("SubtractionDifficulty"); }
if(!PS.MultiProb) { listDataHeader.remove("MultiplicationDifficulty"); }
if(!PS.DivisProb) { listDataHeader.remove("DivisionDifficulty"); }
// Adding child data
List<String> AdditionDifficulty = new ArrayList<>();
while (true) {
if (T1 < 21) {
String S1 = Integer.toString(T1);
AdditionDifficulty.add(S1);
T1++;
} else {
break;
}
}
List<String> SubtractionDifficulty = new ArrayList<>();
while (true) {
if (T2 < 21) {
String S2 = Integer.toString(T2);
SubtractionDifficulty.add(S2);
T2++;
} else {
break;
}
}
List<String> MultiplicationDifficulty = new ArrayList<>();
while (true) {
if (T3 < 21) {
String S3 = Integer.toString(T3);
MultiplicationDifficulty.add(S3);
T3++;
} else {
break;
}
}
List<String> DivisionDifficulty = new ArrayList<>();
while (true) {
if (T4 < 21) {
String S4 = Integer.toString(T4);
DivisionDifficulty.add(S4);
T4++;
} else {
break;
}
}
/** this draws out the Expandable lists **/
if(PS.AddProb) { listDataChild.put(listDataHeader.get(0), AdditionDifficulty); }
if(PS.SubProb) { listDataChild.put(listDataHeader.get(1), SubtractionDifficulty); }
if(PS.MultiProb) { listDataChild.put(listDataHeader.get(2), MultiplicationDifficulty); }
if(PS.DivisProb) { listDataChild.put(listDataHeader.get(3), DivisionDifficulty); }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_difficulty_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
这是评论,不是答案,但我想格式化代码。
不是这样的:
while (true) {
if (T1 < 21) {
String S1 = Integer.toString(T1);
AdditionDifficulty.add(S1);
T1++;
} else {
break;
}
}
更好地写成:
for (; T1 < 21; T1++) {
AdditionDifficulty.add(Integer.toString(T1));
}
不确定为什么T1
是一个字段,因为唯一使用它的代码是此代码。
另请注意,字段名称应位于camelCase
。
因此,如果删除字段T1
,代码将变为:
for (int i = 0; i < 21; i++) {
AdditionDifficulty.add(Integer.toString(i));
}