CustomListViewAdapter应用程序可以保持运营

时间:2015-11-29 16:33:43

标签: android

我在我的android app.it中实现了CustomListview适配器。适用于4个项目。但是当我插入第五个元素时,应用程序崩溃.logcat给出了arrayoutofbounds异常。很有帮助。它是我项目中不可或缺的一部分。这是代码

    MainActivity.java
    public class Dslr extends Activity implements OnItemClickListener
    {
        public static final String[] titles=new String[]{"Nikon D3300","Canon EOS 1200D","Canon EOS 100D (Canon EOS SL1)","Nikon D7200","Nikon D610"};
        public static final String[] Descriptions=new String[]{"The Nikon D3300 is the latest entry-level model to Nikon's series: an affordable and well-balanced choice to introduce you to the world of DSLR.","The Canon EOS 1200D might be a safe replacement of the nearly four-year-old 1100D model, but it holds enough weight to keep the entry-level DSLR market bubbling along.","The Canon EOS 100D sits in a world of its own. It's as small as DSLR cameras come and that in itself is the single biggest reason for buying it","The D7200, complete with a new sensor on board is every bit the Canon EOS 70D competitor, delivering comparable image quality."};
        public static final Integer[] images={ R.drawable.d1,R.drawable.d2,R.drawable.d3,R.drawable.d4,R.drawable.d5};

    ListView listView;
    List<RowItem> rowItems;
    Button back;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dslr);
    back=(Button)findViewById(R.id.button3);
    back.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent s=new Intent(getApplicationContext(),M.class);
            startActivity(s);
            }
    });

    rowItems = new ArrayList<RowItem>();
        for (int i = 0; i < titles.length; i++) {
        RowItem item = new RowItem(images[i], titles[i], Descriptions[i]);
        rowItems.add(item);
        }
        listView=(ListView)findViewById(R.id.list);
        CustomListViewAdapter adapter=new CustomListViewAdapter(this,R.layout.list_item,rowItems);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

    }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,long id)
        {   switch(position)
            {
        case 0: Intent i=new Intent(getApplicationContext(),D.class);
                startActivity(i);
                break;
        case 1:Intent b=new Intent(getApplicationContext(),D_1.class);
                startActivity(b);
                break;
        case 2:Intent c=new Intent(getApplicationContext(),D_2.class);
               startActivity(c);
               break;
        case 3:Intent y=new Intent(getApplicationContext(),D_3.class);
                startActivity(y);
                break;

            }

    }
        }


    CustomListViewAdapter.java
    public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
    Context context;
    public CustomListViewAdapter(Context context,int resourceId,List<RowItem> items)
    {   super(context,resourceId,items);
        this.context=context;
        }
    /*private view holder class*/
    private class ViewHolder {
    ImageView imageView;
    TextView txtTitle;
    TextView txtDesc;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    RowItem rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
    convertView = mInflater.inflate(R.layout.list_item, null);
    holder = new ViewHolder();
    holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
    holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
    holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
    convertView.setTag(holder);
    } else
    holder = (ViewHolder) convertView.getTag();

    holder.txtDesc.setText(rowItem.getDesc());
    holder.txtTitle.setText(rowItem.getTitle());
    holder.imageView.setImageResource(rowItem.getImageId());
    return convertView;

    }
    }

    RowItem.java
    package com.example.m;



    public class RowItem
    {
        private int imageId;
        private String title;
        private String desc;

        public RowItem(int imageId,String title,String desc)
        {
            this.imageId=imageId;
            this.title=title;
            this.desc=desc;
        }
        public int getImageId()
        {
            return imageId;
        }
        public void setImageId(int imageId)
        {
            this.imageId=imageId;
        }
        public String getDesc()
        {
            return desc;
        }
        public String getTitle()
        {
            return title;
        }
        public void setTiltle(String title)
        {
            this.title=title;
        }
        @Override
        public String toString()
        {
            return title + "\n" + desc;
        }

    }

    Dslr.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
     
    <ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    </LinearLayout>

    List_item.xml
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ImageView
    android:id="@+id/icon"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:contentDescription="@string/image"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" />
     
    <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/icon"
    android:paddingBottom="10dp"
    android:textColor="#CC0033"
    android:textSize="16dp" />
     
    <TextView
    android:id="@+id/desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/title"
    android:layout_toRightOf="@+id/icon"
    android:paddingLeft="10dp"
    android:textColor="#3399FF"
    android:textSize="14dp" />
    </RelativeLayout>

Logcat
11-29 21:37:24.325: E/Trace(642): error opening trace file: No such file or directory (2)
11-29 21:37:24.895: D/libEGL(642): loaded /system/lib/egl/libEGL_emulation.so
11-29 21:37:24.895: D/libEGL(642): loaded /system/lib/egl/libGLESv1_CM_emulation.so
11-29 21:37:24.926: D/libEGL(642): loaded /system/lib/egl/libGLESv2_emulation.so
11-29 21:37:24.945: D/(642): HostConnection::get() New Host Connection established 0x2a130b70, tid 642
11-29 21:37:24.995: W/EGL_emulation(642): eglSurfaceAttrib not implemented
11-29 21:37:25.005: D/OpenGLRenderer(642): Enabling debug mode 0
11-29 21:37:48.325: D/dalvikvm(642): GC_CONCURRENT freed 116K, 3% free 8322K/8519K, paused 16ms+32ms, total 112ms
11-29 21:37:48.789: W/EGL_emulation(642): eglSurfaceAttrib not implemented
11-29 21:37:55.817: W/EGL_emulation(642): eglSurfaceAttrib not implemented
11-29 21:37:57.646: D/dalvikvm(642): GC_CONCURRENT freed 125K, 3% free 8606K/8839K, paused 41ms+9ms, total 138ms
11-29 21:37:57.915: D/AndroidRuntime(642): Shutting down VM
11-29 21:37:57.915: W/dalvikvm(642): threadid=1: thread exiting with uncaught exception (group=0x40a122a0)
11-29 21:37:57.935: E/AndroidRuntime(642): FATAL EXCEPTION: main
11-29 21:37:57.935: E/AndroidRuntime(642): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.m/com.example.m.Dslr}: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
11-29 21:37:57.935: E/AndroidRuntime(642):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-29 21:37:57.935: E/AndroidRuntime(642):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-29 21:37:57.935: E/AndroidRuntime(642):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-29 21:37:57.935: E/AndroidRuntime(642):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-29 21:37:57.935: E/AndroidRuntime(642):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 21:37:57.935: E/AndroidRuntime(642):  at android.os.Looper.loop(Looper.java:137)
11-29 21:37:57.935: E/AndroidRuntime(642):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-29 21:37:57.935: E/AndroidRuntime(642):  at java.lang.reflect.Method.invokeNative(Native Method)
11-29 21:37:57.935: E/AndroidRuntime(642):  at java.lang.reflect.Method.invoke(Method.java:511)
11-29 21:37:57.935: E/AndroidRuntime(642):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-29 21:37:57.935: E/AndroidRuntime(642):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-29 21:37:57.935: E/AndroidRuntime(642):  at dalvik.system.NativeStart.main(Native Method)
11-29 21:37:57.935: E/AndroidRuntime(642): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
11-29 21:37:57.935: E/AndroidRuntime(642):  at com.example.m.Dslr.onCreate(Dslr.java:60)
11-29 21:37:57.935: E/AndroidRuntime(642):  at android.app.Activity.performCreate(Activity.java:5008)
11-29 21:37:57.935: E/AndroidRuntime(642):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-29 21:37:57.935: E/AndroidRuntime(642):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-29 21:37:57.935: E/AndroidRuntime(642):  ... 11 more

3 个答案:

答案 0 :(得分:2)

我发现三个数组:projectB:projectC

中没有相同的元素

确保三个数组中的元素相同。

答案 1 :(得分:0)

描述只有4个项目,而不是5个。

答案 2 :(得分:0)

您的说明数组只有四个项目,您需要为每个数组添加相同数量的项目。