为什么以下代码给出了ArrayIndexOutofBounds Exception?

时间:2015-07-18 14:11:49

标签: android exception

我想制作一个自定义列表视图,其中包含一个图像,两个文本字段和一个按钮。虽然,我处理了两个文本字段和按钮,但插入图像会产生问题。谁能告诉我我的代码中有什么问题? 见代码......

MainActivity.java

package com.apna.mycontacts;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{
ListView lv;
ArrayList<String> first= new ArrayList<String>(Arrays.asList("Ankit","Abhishek","Pinak","Arun","AK"));
ArrayList<String> last= new ArrayList<String>(Arrays.asList("Arora","Bhardwaj","jakhar","jadav","AK"));
List<Integer> imag=new ArrayList<Integer>(Arrays.asList(
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher));
Button add;
CustomAdapter cd;
TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView) findViewById(R.id.listView1);
        cd=new CustomAdapter(MainActivity.this,first,last,imag);
        tv=(TextView) findViewById(R.id.textView1);
    add=(Button) findViewById(R.id.button1);
    add.setOnClickListener(this);
    lv.setAdapter(cd);
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent in=new Intent(MainActivity.this,Add.class);
        startActivityForResult(in,5);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==5)
    {
        String a=data.getStringExtra("fname");
        String b=data.getStringExtra("lname");
        String c=data.getStringExtra("image");
        tv.setText(c);
        first.add(a);
        last.add(b);
        cd.notifyDataSetChanged();

    }
    }
}

CustomAdapter.java

package com.apna.mycontacts;

import java.util.ArrayList;
import java.util.List;

import com.apna.mycontacts.R.layout;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter<String> 
{
Context con;
    ArrayList<String> name,Last;
    List<Integer> imag;
    public CustomAdapter(Context con,ArrayList<String> first, ArrayList<String> last,List<Integer> image) {
        // TODO Auto-generated constructor stub

        super(con,R.layout.second,first);
        name=first;
        this.con=con;
        imag=image;
        Last=last;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater li=(LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView=li.inflate(R.layout.second,null);
        TextView tv1=(TextView) convertView.findViewById(R.id.t1);
        TextView tv2=(TextView) convertView.findViewById(R.id.t2);
        ImageView im=(ImageView) convertView.findViewById(R.id.imageView1);

        Button b=(Button) convertView.findViewById(R.id.button1);
        tv1.setText(name.get(position));
        tv2.setText(Last.get(position));
        im.setImageResource(imag.get(position));

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                name.remove(position);
                Last.remove(position);
                imag.remove(position);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }


}

错误日志:

07-18 13:41:40.372: E/AndroidRuntime(755): FATAL EXCEPTION: main
07-18 13:41:40.372: E/AndroidRuntime(755): java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
07-18 13:41:40.372: E/AndroidRuntime(755):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
07-18 13:41:40.372: E/AndroidRuntime(755):  at java.util.ArrayList.get(ArrayList.java:304)
07-18 13:41:40.372: E/AndroidRuntime(755):  at com.apna.mycontacts.CustomAdapter.getView(CustomAdapter.java:55)
07-18 13:41:40.372: E/AndroidRuntime(755):  at android.widget.AbsListView.obtainView(AbsListView.java:2143)
07-18 13:41:40.372: E/AndroidRuntime(755):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
07-18 13:41:40.372: E/AndroidRuntime(755):  at android.widget.ListView.onMeasure(ListView.java:1158)
07-18 13:41:40.372: E/AndroidRuntime(755):  at android.view.View.measure(View.java:15518)
07-18 13:41:40.372: E/AndroidRuntime(755):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
07-18 13:41:40.372: E/AndroidRuntime(755):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
07-18 13:41:40.372: E/AndroidRuntime(755):  at android.view.View.measure(View.java:15518)
07-18 13:41:40.372: E/AndroidRuntime(755):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)

3 个答案:

答案 0 :(得分:2)

  

java.lang.IndexOutOfBoundsException:索引4无效,大小为4

您的列表中有4个元素,但您尝试访问第五个元素。

记住列表元素从索引 开始,因此要访问列表中您需要检索索引元素 3 的第4个元素。

答案 1 :(得分:0)

添加另一个与您的字符串大小相等的图像。

答案 2 :(得分:0)

这是问题.... 您没有3个equel大小的列表。 改变它..

List<Integer> imag=new ArrayList<Integer>(Arrays.asList(
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher));

List<Integer> imag=new ArrayList<Integer>(Arrays.asList(
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher));

多数民众赞成......