1. 程式人生 > >android Listview顯示不同的item遇到的問題

android Listview顯示不同的item遇到的問題

11-21 23:01:23.450: E/AndroidRuntime(31626): FATAL EXCEPTION: main
11-21 23:01:23.450: E/AndroidRuntime(31626): java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
11-21 23:01:23.450: E/AndroidRuntime(31626):    at android.widget.AbsListView$RecycleBin.scrapActiveViews(AbsListView.java:7206)
11-21 23:01
:23.450: E/AndroidRuntime(31626): at android.widget.ListView.layoutChildren(ListView.java:1809) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.widget.AbsListView.onLayout(AbsListView.java:2076) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.View.layout(View.java:14159) 11-21 23
:01:23.450: E/AndroidRuntime(31626): at android.view.ViewGroup.layout(ViewGroup.java:4402) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:985) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.View.layout(View.java:14159) 11-21 23
:01:23.450: E/AndroidRuntime(31626): at android.view.ViewGroup.layout(ViewGroup.java:4402) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.widget.ScrollView.onLayout(ScrollView.java:1632) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.View.layout(View.java:14159) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.ViewGroup.layout(ViewGroup.java:4402) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.View.layout(View.java:14159) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.ViewGroup.layout(ViewGroup.java:4402) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.View.layout(View.java:14159) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.ViewGroup.layout(ViewGroup.java:4402) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.View.layout(View.java:14159) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.ViewGroup.layout(ViewGroup.java:4402) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.View.layout(View.java:14159) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.ViewGroup.layout(ViewGroup.java:4402) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.View.layout(View.java:14159) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.ViewGroup.layout(ViewGroup.java:4402) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1944) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1763) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4468) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.Choreographer.doCallbacks(Choreographer.java:562) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.Choreographer.doFrame(Choreographer.java:532) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.os.Handler.handleCallback(Handler.java:725) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.os.Handler.dispatchMessage(Handler.java:92) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.os.Looper.loop(Looper.java:137) 11-21 23:01:23.450: E/AndroidRuntime(31626): at android.app.ActivityThread.main(ActivityThread.java:5068) 11-21 23:01:23.450: E/AndroidRuntime(31626): at java.lang.reflect.Method.invokeNative(Native Method) 11-21 23:01:23.450: E/AndroidRuntime(31626): at java.lang.reflect.Method.invoke(Method.java:511) 11-21 23:01:23.450: E/AndroidRuntime(31626): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 11-21 23:01:23.450: E/AndroidRuntime(31626): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 11-21 23:01:23.450: E/AndroidRuntime(31626): at dalvik.system.NativeStart.main(Native Method) 當做listview適配不同的佈局時出現上面的錯誤,以下是我的造成錯誤的程式碼

public List getListData() {
list = new ArrayList();
for (int i = 1; i < 6; i++) {
TypeBean bean = new TypeBean();

         if (i == 1) {
            bean.type = 1;
        } else if (i == 2) {
            bean.type = 2;
        } else if (i == 3) {
            bean.type = 3;
        } else if (i == 4) {
            bean.type = 4;
        } else if (i == 5) {
            bean.type = 5;
        } 
        list.add(bean);
    }
    return list;
}

錯誤的介面卡裡面的資料索引設定

下面的程式碼是我修改過後的解決問題的程式碼
修改後的的程式碼

修改後的介面卡裡面的索引值