当前位置: 代码迷 >> Android >> 创建TextView会导致异常
  详细解决方案

创建TextView会导致异常

热度:36   发布时间:2023-08-04 10:59:26.0

我创建了一个显示此代码结果的适配器。

public class Adapter_roadmap extends ArrayAdapter<String> {
        private final Context context;
        private final  ArrayList<String> station_hour, station_hour_passed, station_name, station_name_passed, station_phoneNum;
        private final ArrayList<Boolean> station_passed;
        public   ArrayList<String> 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(position));
textView_Hour_passed.setText(station_hour_passed.get(position));

当我尝试删除它们时,也不例外。 我需要这两行,它们是我的应用程序的批评者。 为什么要创建一个额外的索引? 有什么建议么?

它抛出IndexOutOfBoundsException 这意味着您正在尝试从不存在的数组中获取数据。

该异常指出Invalid index 4, size is 4 它告诉您索引为4,大小仅为4。(当然,请注意数组从0开始。因此,第4个索引正在尝试查找大小至少为5的东西)。

我建议你检查一下:

  1. 所有数据都在数组station_name_passedstation_hour_passed
  2. 您不想设置的字段多于拥有的数据
  3. 您从0开始索引
  4. 您正在使用正确的索引。 您正在使用所有4个数组的头寸。

关于第4点; 看来您有名称和营业时间,有条件地传递给Names_passed和Hour_passed。 我只是在猜测,但是如果仅输入传递的名称和小时数,则最终会减少名称和小时数。 这意味着该数组的大小较小。 遍历所有名称和小时数最终将导致传递的数组超出范围,因为它们与原始名称和小时数集相比具有更少的条目

  相关解决方案