我收到一个IndexOutOfBoundsException,我需要一些帮助。
这是我的堆栈跟踪中的错误
sudo service jetty8 start
我知道为什么会发生异常,因为在我的JSON文件中,machinereports数组有不同数量的项目,如果你看下面,第一项有4项,第二项有6,
FATAL EXCEPTION: main
Process: com.guhring.vending, PID: 2210
java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.guhring.vending.adapters.ReportAdapter.getView(ReportAdapter.java:112)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1280)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
这是我的主要课程活动和报告类
{
"id" : 1,
"reports": [
{
"id": "1",
"title": "For Reorder",
"subtitle": "Report Name",
"date": "Monday, Aug 08, 2016",
"machinereports": [
{
"name": "Reorder List",
"count": "9"
},
{
"name": "Reorder List Critical",
"count": "9"
}
]
},
{
"id": "2",
"title": "For Stock",
"subtitle": "Report Name",
"date": "Monday, Aug 08, 2016",
"machinereports": [
{
"name": "Article List",
"count": "531"
},
{
"name": "Articles Out For More Than 7 Days",
"count": "1"
},
{
"name": "Stock Report",
"count": "559"
}
]
}
]
}
我的自定义适配器设置如下
public class MachineReportsActivity extends AppCompatActivity {
private ReportAdapter adapter;
private ListView mListView;
private ArrayList<Report> reportList;
private String id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
setContentView(R.layout.machinereports);
Intent i = getIntent();
id = i.getStringExtra("id");
mListView = (ListView) findViewById(R.id.machine_report_list_view);
reportList = Report.getReportsFromFile("reports.json", this);
adapter = new ReportAdapter(this, reportList);
if (mListView != null) {
mListView.setAdapter(adapter);
}
}
}
public class Report {
public String id;
public String title;
public String subtitle;
public String date;
public ArrayList<String> machinereports = new ArrayList<>();
public static ArrayList<Report> getReportsFromFile(String filename, Context context) {
final ArrayList<Report> reportList = new ArrayList<>();
try {
// Load Data
String jsonStr = loadJsonFromAsset("reports.json", context);
JSONObject jsonOne = new JSONObject(jsonStr);
JSONArray reports = jsonOne.getJSONArray("reports");
// Get Report objects from data
for(int i = 0; i < reports.length(); i++) {
Report report = new Report();
report.id = reports.getJSONObject(i).getString("id");
report.title = reports.getJSONObject(i).getString("title");
report.subtitle = reports.getJSONObject(i).getString("subtitle");
report.date = reports.getJSONObject(i).getString("date");
// Get inner array listOrReports
/* JSONArray rList = jsonOne.getJSONArray("machinereports");*/
JSONArray rList = reports.getJSONObject(i).getJSONArray("machinereports");
for(int j = 0; j < rList.length(); j++) {
JSONObject jsonTwo = rList.getJSONObject(j);
report.machinereports.add(jsonTwo.getString("name"));
report.machinereports.add(jsonTwo.getString("count"));
}
reportList.add(report);
}
} catch (JSONException e) {
e.printStackTrace();
}
return reportList;
}
private static String loadJsonFromAsset(String filename, Context context) {
String json = null;
try {
InputStream is = context.getAssets().open(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
}
catch (java.io.IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
这是我的列表项视图,在视图中我想根据从json对象返回的机器报告项的数量填充两个texviews,但正如您所看到的,我设置它的方式,它不允许我这样做。我必须编写n个RelativeLayout实例并提供数据,但机器报告数据计数是可变的。
public class ReportAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private ArrayList<Report> mDataSource;
private ArrayList<Report> arrayList;
public ReportAdapter(Context context, ArrayList<Report> items) {
mContext = context;
mDataSource = items;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arrayList = new ArrayList<Report>();
arrayList.addAll(mDataSource);
}
// Implement a ViewHolder Class which stores each of the row's subviews,
// and in turn is stored inside the tag field of the layout
private static class ViewHolder {
public TextView titleTextView;
public TextView subTitleTextView;
public TextView dateTextView;
public TextView reportOneTextView;
public TextView reportTwoTextView;
public TextView reportThreeTextView;
public TextView reportFourTextView;
public TextView reportFiveTextView;
public TextView reportSixTextView;
}
@Override
public int getCount() {
return mDataSource.size();
}
@Override
public Object getItem(int position) {
return mDataSource.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_machine_reports, parent, false);
holder = new ViewHolder();
holder.titleTextView = (TextView) convertView.findViewById(R.id.reports_list_title);
holder.subTitleTextView = (TextView) convertView.findViewById(R.id.reports_list_subtitle);
holder.dateTextView = (TextView) convertView.findViewById(R.id.reports_list_date);
holder.reportOneTextView = (TextView) convertView.findViewById(R.id.reports_list_reportName);
holder.reportTwoTextView = (TextView) convertView.findViewById(R.id.reports_list_reportCount);
holder.reportThreeTextView = (TextView) convertView.findViewById(R.id.reports_list_reportName02);
holder.reportFourTextView = (TextView) convertView.findViewById(R.id.reports_list_reportCount02);
holder.reportFiveTextView = (TextView) convertView.findViewById(R.id.reports_list_reportName03);
holder.reportSixTextView = (TextView) convertView.findViewById(R.id.reports_list_reportCount03);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
TextView titleTextView = holder.titleTextView;
TextView subTitleTextView = holder.subTitleTextView;
TextView dateTextView = holder.dateTextView;
TextView reportOneTextView = holder.reportOneTextView;
TextView reportTwoTextView = holder.reportTwoTextView;
TextView reportThreeTextView = holder.reportThreeTextView;
TextView reportFourTextView = holder.reportFourTextView;
TextView reportFiveTextView = holder.reportFiveTextView;
TextView reportSixTextView = holder.reportSixTextView;
Report report = (Report) getItem(position);
titleTextView.setText(report.title);
subTitleTextView.setText(report.subtitle);
dateTextView.setText(report.date);
reportOneTextView.setText(report.machinereports.get(0));
reportTwoTextView.setText(report.machinereports.get(1));
reportThreeTextView.setText(report.machinereports.get(2));
reportFourTextView.setText(report.machinereports.get(3));
reportFiveTextView.setText(report.machinereports.get(4));
reportSixTextView.setText(report.machinereports.get(5));
return convertView;
}
}