为什么我的数组没有出现在ListView中?

时间:2015-01-31 01:46:39

标签: java android arrays android-activity arraylist

当我单击应用程序菜单中的某个按钮时,直接输出,然后在LogCat中出现此错误:

01-31 08:33:53.007: E/AndroidRuntime(2481): FATAL EXCEPTION: main
01-31 08:33:53.007: E/AndroidRuntime(2481): java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
01-31 08:33:53.007: E/AndroidRuntime(2481):     at com.bongkorr.ItemListBaseAdapter.getView(ItemListBaseAdapter.java:59)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.widget.AbsListView.obtainView(AbsListView.java:2143)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.widget.ListView.onMeasure(ListView.java:1158)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.View.measure(View.java:15518)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.View.measure(View.java:15518)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.View.measure(View.java:15518)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.View.measure(View.java:15518)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.View.measure(View.java:15518)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.Choreographer.doFrame(Choreographer.java:532)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.os.Handler.handleCallback(Handler.java:725)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.os.Looper.loop(Looper.java:137)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at java.lang.reflect.Method.invokeNative(Native Method)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at java.lang.reflect.Method.invoke(Method.java:511)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-31 08:33:53.007: E/AndroidRuntime(2481):     at dalvik.system.NativeStart.main(Native Method)

我的代码有问题吗?

Informasi.java

public class Informasi extends Activity {
    protected static final ItemDetails ItemDetails = null;
    protected Object Object;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.informasi);

        ArrayList<ItemDetails> image_details = GetSearchResults();

        final ListView lv1 = (ListView) findViewById(R.id.listV_main);
        lv1.setAdapter(new ItemListBaseAdapter(this, image_details));

        lv1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) { 

                Intent i = null; 
                if (position == 0){ 
                    i = new Intent(Informasi.this,A1.class);
                    } 
                else if(position == 1){ 
                    i = new Intent(Informasi.this,A1A.class); 
                    }
                else if(position == 2){ 
                    i = new Intent(Informasi.this,A2.class);
                    }       
                else if(position == 3){ 
                    i = new Intent(Informasi.this,A3.class);
                    }       
                else if(position == 4){ 
                    i = new Intent(Informasi.this,A4.class);

                        } 
                else{} 
                startActivity(i);      
            }
        });
    }

    private ArrayList<ItemDetails> GetSearchResults(){
        ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();

        ItemDetails item_details = new ItemDetails();
        item_details.setName("Angkot 01");
        item_details.setItemDescription("CipinangGading-Terminal Merdeka");
        item_details.setImageNumber(1);
        results.add(item_details);

        item_details = new ItemDetails();
        item_details.setName("Angkot 01A");
        item_details.setItemDescription("Barangsiang-Ciawi");
        item_details.setImageNumber(2);
        results.add(item_details);

        item_details = new ItemDetails();
        item_details.setName("Angkot 02");
        item_details.setItemDescription("Sukasari-Batutulis-Terminal Bubulak");
        item_details.setImageNumber(3);
        results.add(item_details);

        item_details = new ItemDetails();
        item_details.setName("Angkot 03");
        item_details.setItemDescription("Baranangsiang-Terminal Bubulak");
        item_details.setImageNumber(4);
        results.add(item_details);

        item_details = new ItemDetails();
        item_details.setName("Angkot 04");
        item_details.setItemDescription("Ramayana-Rancamaya");
        item_details.setImageNumber(5);
        results.add(item_details);




    return results;


}
}

ItemListBaseAdapter.java

    public class ItemListBaseAdapter extends BaseAdapter {
    private static ArrayList<ItemDetails> itemDetailsrrayList;

    private Integer[] imgid = {
            R.drawable.a1a,
            R.drawable.a1,
            R.drawable.a2,
            R.drawable.a3,
            R.drawable.a4,

            };

    private LayoutInflater l_Inflater;

    public ItemListBaseAdapter(Context context, ArrayList<ItemDetails> results) {
        itemDetailsrrayList = results;
        l_Inflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return itemDetailsrrayList.size();
    }

    public Object getItem(int position) {
        return itemDetailsrrayList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = l_Inflater.inflate(R.layout.modelinfo, null);
            holder = new ViewHolder();
            holder.txt_itemName = (TextView) convertView.findViewById(R.id.item_nama);
            holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.item_keterangan);
            holder.itemImage = (ImageView) convertView.findViewById(R.id.item_icon);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName());
        holder.txt_itemDescription.setText(itemDetailsrrayList.get(position).getItemDescription());
        holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get(position).getImageNumber() - 6]);
//      imageLoader.DisplayImage("http://192.168.1.28:8082/ANDROID/images/BEVE.jpeg", holder.itemImage);

        return convertView;
    }

    static class ViewHolder {
        TextView txt_itemName;
        TextView txt_itemDescription;
        ImageView itemImage;
    }
}

ItemDetails.java

public class ItemDetails {

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getItemDescription() {
        return itemDescription;
    }
    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDescription;
    }

    public int getImageNumber() {
        return imageNumber;
    }
    public void setImageNumber(int imageNumber) {
        this.imageNumber = imageNumber;
    }

    private String name ;
    private String itemDescription;
    private int imageNumber;


}

是否因阵列错误而发生?请帮帮我,我还没找到解决方案

1 个答案:

答案 0 :(得分:0)

line:holder.itemImage.setImageResource(imgid [itemDetailsrrayList.get(position).getImageNumber() - 6]);

导致异常