我已经创建了一个显示此代码结果的适配器。 public class Adapter_roadmap extends ArrayAdapter { 私人最终上下文背景; private final ArrayList station_hour,station_hour_passed,station_name,station_name_passed,station_phoneNum; private final ArrayList station_passed; public ArrayList Station_hour_past,Station_hour_future,Station_name_past,Station_name_future,Station_phoneNum_past,Station_phoneNum_future;
public Adapter_roadmap(Context context, ArrayList<String> station_name_values, ArrayList<String> station_name_passed_values, ArrayList<String> station_hour_values, ArrayList<String> station_hour__passed_values, ArrayList<String> station_phoneNum_values, ArrayList<Boolean> station_passed_values) {
super(context, R.layout.listitem_road_map, station_name_values);
this.context = context;
System.out.println("initial past size: " + station_name_passed_values.size());
System.out.println("initial past size: " + station_hour__passed_values.size());
station_name = station_name_values;
station_name_passed = station_name_passed_values;
station_hour = station_hour_values;
station_hour_passed = station_hour__passed_values;
station_phoneNum = station_phoneNum_values;
station_passed = station_passed_values;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
System.out.println("start of getView");
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.listitem_road_map, parent, false);
TextView textView_Names_passed = (TextView) rowView.findViewById(R.id.LI_TV_Name_Passed);
TextView textView_Hour_passed = (TextView) rowView.findViewById(R.id.LI_TV_Hour_passed);
TextView textView_Names = (TextView) rowView.findViewById(R.id.LI_TV_Name);
TextView textView_Hour = (TextView) rowView.findViewById(R.id.LI_TV_Hour);
textView_Names_passed.setText(station_name_passed.get(position));
textView_Hour_passed.setText(station_hour_passed.get(position));
textView_Names.setText(station_name.get(position));
textView_Hour.setText(station_hour.get(position));
当我尝试运行代码时,遇到以下异常:
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: FATAL EXCEPTION: main
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: Process: com.myt.mytguideapp, PID: 29764
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at java.util.ArrayList.get(ArrayList.java:308)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at Adapters.Adapter_roadmap.getView(Adapter_roadmap.java:90)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.widget.AbsListView.obtainView(AbsListView.java:2255)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.widget.ListView.onMeasure(ListView.java:1175)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.View.measure(View.java:16497)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.View.measure(View.java:16497)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.View.measure(View.java:16497)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.View.measure(View.java:16497)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.View.measure(View.java:16497)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:574)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:544)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5001)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-22 11:49:04.748 29764-29764/com.myt.mytguideapp E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
当我尝试调试时,我注意到这些特定的两行中发生了异常:
textView_Names_passed.setText(station_name_passed.get(位置)); textView_Hour_passed.setText(station_hour_passed.get(位置));
当我尝试删除它们时,没有例外。 我需要这两行,他们是我的应用程序的批评。 为什么要创建额外的索引? 有什么建议吗?
答案 0 :(得分:0)
投掷IndexOutOfBoundsException
。这意味着您正在尝试从不存在的数组中获取数据。
该例外指出Invalid index 4, size is 4
。这告诉你你在索引4.并且大小只有4.(当然注意数组从0开始。所以第四个索引试图寻找至少大小为5的东西。)
我建议您检查一下:
station_name_passed
中设置
station_hour_passed
关于第4点;您似乎有条件地传递给Names_passed和Hour_passed的名称和小时数。我只是猜测这一点,但如果你只输入通过的名字和小时数,你的名字和小时数就会减少。这意味着您拥有较小的Array数量。循环遍历所有名称和小时将最终导致传递的数组超出界限,因为它们具有较少的条目作为原始名称和小时数