1

我收到一个 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;
        }

}

使用列表视图的片段代码是这样的

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];

我哪里错了?谢谢

4

2 回答 2

1

这是因为你data[]static。当您创建一个属性static时,它会使从该类派生的所有对象的属性相同。要修复它,只需将数据设为 not static

于 2013-07-18T20:52:33.620 回答
1

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

于 2013-07-18T20:53:05.497 回答