1

I am getting an arrayindexoutofbounds exception. My adapter code is

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

}

and the code for the fragment using the listview is this

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

I am getting the arrayindexoutofbounds exception in this line

    Level weather = data[position];

Where am i going wrong? Thanks

2 Answers 2

1

It is because your data[] is static. When you make a property static, it makes the property the same for all objects derived from that class. To fix it, just make data not static.

Sign up to request clarification or add additional context in comments.

1 Comment

No problem, and also you should make your context and layoutResourceId non-static as well to prevent future problems. Don't add modifiers if you don't need them.
1

Member variables marked as static exist only once regardless how many object instances you create. Both adapters share the same data member.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.