使用listview时android arrayindexoutofbounds异常

时间:2013-07-18 20:48:36

标签: android android-listview android-adapter indexoutofboundsexception

我得到一个arrayindexoutofbounds异常。我的适配器代码是

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LevelAdapter extends ArrayAdapter<Level> {

     static Context context;
        static int layoutResourceId;   
        static Level data[] = null;

     public LevelAdapter(Context context, int layoutResourceId, Level[] data) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;
        }

       /* public LevelAdapter(ShowFrag1 showFrag1, int listItem,
                Level[] weather_data) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;

            // TODO Auto-generated constructor stub
        }*/

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            WeatherHolder holder = null;

            if(row == null)
            {
                LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                row = inflater.inflate(layoutResourceId, parent, false);

                holder = new WeatherHolder();
              // holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
                holder.txtTitle = (TextView)row.findViewById(R.id.txt);
               // holder.imgIcon2=(ImageView)row.findViewById(R.id.imgIcon2);
                Typeface robotoLight = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Light.ttf");
                holder.txtTitle.setTypeface(robotoLight);

                row.setTag(holder);
            }
            else
            {
                holder = (WeatherHolder)row.getTag();
            }

            Level weather = data[position];
            holder.txtTitle.setText(weather.title);
        //    holder.imgIcon.setImageResource(weather.icon);

            return row;
        }

        static class WeatherHolder
        {
         //   ImageView imgIcon;
            TextView txtTitle;
        //    ImageView imgIcon2;
        }

}

使用listview的片段代码是这个

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AboutFrag3 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v=inflater.inflate(R.layout.aboutfrag3, container,false);
        ListView lv1=(ListView) v.findViewById(R.id.listView1);
        ListView lv2=(ListView)v.findViewById(R.id.listView2);
        Level weather_data[] = new Level[]
                {

                    new Level(R.drawable.sac1, "O63", R.drawable.play_button),
                    new Level(R.drawable.sn2, "O26",R.drawable.play_button),
                    new Level(R.drawable.sn3, "O",R.drawable.play_button),
                    new Level(R.drawable.sn4, "O83",R.drawable.play_button),
                    new Level(R.drawable.sn6, "O.23",R.drawable.play_button),
                    new Level(R.drawable.sn6, "O9",R.drawable.play_button),
                    new Level(R.drawable.sn6, "O96",R.drawable.play_button),
                    new Level(R.drawable.sn6, "T98",R.drawable.play_button),
                    new Level(R.drawable.sn6, "T37",R.drawable.play_button),
                    new Level(R.drawable.sn6, "T248",R.drawable.play_button),
                    new Level(R.drawable.sn6, "T.87",R.drawable.play_button),
                    new Level(R.drawable.s6, "Tee:   54.06",R.drawable.play_button),
                    new Level(R.drawable.sn6, "T1",R.drawable.play_button),
                    new Level(R.drawable.s6, "Te",R.drawable.play_button)


                };
        Level weather_data2[] = new Level[]
                {
                new Level(R.drawable.s1,"O3",R.drawable.play_button),
                new Level(R.drawable.s1,"O   154",R.drawable.play_button),
                new Level(R.drawable.s1,"O2",R.drawable.play_button),
                new Level(R.drawable.sh1,"O:   5.11",R.drawable.play_button),
                new Level(R.drawable.s1, "Te8",R.drawable.play_button),
                new Level(R.drawable.s1, "T5",R.drawable.play_button),
                new Level(R.drawable.s1, "T-10",R.drawable.play_button),
                new Level(R.drawable.s1, "T52",R.drawable.play_button)
                };
        LevelAdapter adapter = new LevelAdapter(getActivity(),
                R.layout.list_item, weather_data);
        LevelAdapter adapter2 = new LevelAdapter(getActivity(), R.layout.list_item, weather_data2);

        View header = inflater.inflate(R.layout.header2, null);
        //View header2 = inflater.inflate(R.layout.header, null);
        View header3 = inflater.inflate(R.layout.header3, null);
       lv1.addHeaderView(header);
       lv2.addHeaderView(header3);
        lv1.setAdapter(adapter);
        lv2.setAdapter(adapter2);
        Utility.setListViewHeightBasedOnChildren(lv1);
        Utility.setListViewHeightBasedOnChildren(lv2);
        return v;
    }
}

我在这一行中得到了arrayindexoutofbounds异常

    Level weather = data[position];

我哪里错了? 感谢

2 个答案:

答案 0 :(得分:1)

这是因为您的data[]static。创建属性static时,它会使该属性与从该类派生的所有对象相同。要解决此问题,只需将数据设为static

答案 1 :(得分:1)

标记为static的成员变量只存在一次,无论您创建了多少个对象实例。两个适配器共享相同的data成员。