为什么我的程序给出了Array Index of Bound,即使我正确排列索引?

时间:2016-05-21 11:37:55

标签: android

每当我启动我的应用程序时,我都会收到错误,这是我试图解决的几个小时的数组索引,但我不能。请帮忙

  

java.lang.ArrayIndexOutOfBoundsException:length = 100; index = 100 at com.arksoft.maneviyatvakti.MyAdapter.getView(MainActivity.java:308)

此处出现错误的第308行

imageView.setImageResource(images[position]);

完整代码:

public class MainActivity extends Activity {



    public  MyAdapter adapter;
    private View view;
    Context context;
    public  ListView list;

    public int t[];

    public SharedPreferences prefs;
    public SharedPreferences.Editor edit;
    public SharedPreferences preferences;
    public SharedPreferences.Editor editor;


    int [] btnNums={66,150,94,72,206,489,1481,731,170,91,145,137,662,770,14,351,258,298,131,180,1281,306,14,308
            ,19,134,514,305,68,104,129,62,47,409,630,744,131,184,846,37,336,812,88,1020,1286,1106,551,202,156,206,
            662,731,1281,801,351,117,770,302,526,212,1100,1060,1100,232,998,209,80,73,270,312,55,110,78,209
            ,114,161,1001,400,3246,573,319,108,66,116,500,2116,68,286,201,256,113,137,148,56,124,68,490,324,156,14,48};
   /* int [] tuesdaynum={};
    int [] wednesdaynum={};
    int [] thursdaynum={};
    int [] fridaynum={};

    int [] saturdaynum={};
    int [] sundaynum={};

    int [] btnNumsrefs={};



*/

    int yesterday;
    int [] btnNumsrefs={66,150,94,72,206,489,1481,731,170,91,145,137,662,770,14,351,258,298,131,180,1281,306,14,308
            ,19,134,514,305,68,104,129,62,47,409,630,744,131,184,846,37,336,812,88,1020,1286,1106,551,202,156,206,
            662,731,1281,801,351,117,770,302,526,212,1100,1060,1100,232,998,209,80,73,270,312,55,110,78,209
            ,114,161,1001,400,3246,573,319,108,66,116,500,2116,68,286,201,256,113,137,148,56,124,68,490,324,156,14,48};
    public void storeYesterday(Calendar a){
        yesterday=a.get(Calendar.DAY_OF_MONTH);

        preferences=getSharedPreferences("yesterday",0);
        editor=preferences.edit();
        editor.putInt("yesterday",yesterday);

        editor.commit();

    }





    public Integer getFromYesterday(){
        preferences=getSharedPreferences("yesterday",0);
        int get =preferences.getInt("yesterday", 0);

        return get;
    }





    int[] images = {R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7, R.drawable.a8, R.drawable.a9,
            R.drawable.a10, R.drawable.a11, R.drawable.a12, R.drawable.a13, R.drawable.a14,R.drawable.a15,R.drawable.a16,R.drawable.a17,R.drawable.a18,R.drawable.a19,R.drawable.a20,R.drawable.a21,R.drawable.elkahhar, R.drawable.elvehhab, R.drawable.errezzak, R.drawable.elvahid, R.drawable.essamed,
            R.drawable.elkabid, R.drawable.elkadir, R.drawable.elhakem, R.drawable.eladl,

            R.drawable.ellatif, R.drawable.elbatin, R.drawable.elvali, R.drawable.ettevvab, R.drawable.elmuntekim,R.drawable.elmuktedir, R.drawable.esselam, R.drawable.elmukaddim, R.drawable.elmuaahir, R.drawable.elevvel, R.drawable.elmusavvir, R.drawable.elhabir, R.drawable.elhalim, R.drawable.elazim,

            R.drawable.elgafur, R.drawable.ezzahir, R.drawable.elmuteali, R.drawable.elberr, R.drawable.elafuv,R.drawable.elcebbar, R.drawable.elmutekebbir2, R.drawable.elhaliik, R.drawable.ellahir, R.drawable.errafi, R.drawable.elmuiz, R.drawable.elmuzil, R.drawable.elbasir,

            R.drawable.essekur, R.drawable.malikulmulk, R.drawable.zulcelalilikram, R.drawable.elganiy, R.drawable.elmugni,R.drawable.elkebir, R.drawable.elhafiz, R.drawable.elmukit, R.drawable.elhasib, R.drawable.elcelil, R.drawable.elkerim, R.drawable.elrakib, R.drawable.elmucib, R.drawable.elaliy,

            R.drawable.elhakim, R.drawable.elmuksit, R.drawable.elcami, R.drawable.elmani, R.drawable.eddar,R.drawable.elvedud, R.drawable.elmecid, R.drawable.elbais, R.drawable.essehid, R.drawable.elhak, R.drawable.elvekil, R.drawable.elkaviy, R.drawable.elmetin, R.drawable.elveliy,

            R.drawable.elhamid, R.drawable.errauf, R.drawable.ennafi, R.drawable.ennur, R.drawable.elbaki,R.drawable.elvasi, R.drawable.elmuhsi, R.drawable.elmubdi, R.drawable.elmuid, R.drawable.elmuhyi, R.drawable.elmumit, R.drawable.elhay, R.drawable.elkayyum, R.drawable.elvacid,

            R.drawable.elmacid};





    String[] exp;
    String[] exp2;
    String[] exp3;
    String[] exp4;
    String[] exp5;
    String[] exp6;
    String[] exp7;
    String[] tsd,wed,thrs,frd,sat,sndy;

    String[] mean;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        edit = this.getSharedPreferences("NAME", Context.MODE_APPEND).edit();
        prefs = getSharedPreferences("NAME", Context.MODE_APPEND);
        view = getWindow().getDecorView().getRootView();
        Admob.createLoadBanner(getApplicationContext(), view);
        Admob.createLoadInterstitial(getApplicationContext(), null);


        Resources res = getResources();







        exp = res.getStringArray(R.array.fazilet);
        exp2 = res.getStringArray(R.array.faz2);
        exp3 = res.getStringArray(R.array.faz3);
        exp4 = res.getStringArray(R.array.faz4);
        exp5 = res.getStringArray(R.array.faz5);
        exp6 = res.getStringArray(R.array.faz6);
        exp7 = res.getStringArray(R.array.faz7);
        mean = res.getStringArray(R.array.anlam);
        list = (ListView) findViewById(R.id.listView);

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(new Date());


        int today = cal.get(Calendar.DAY_OF_MONTH);
        if(today==Calendar.MONDAY) {


        }


        if (getFromYesterday() == null) {
            storeYesterday(cal);

        } else {
            if (today != getFromYesterday()) {


                storeYesterday(cal);

                adapter = new MyAdapter(this, exp, images, mean, btnNums);
                storeYesterday(cal);

                edit.putInt("Count", btnNumsrefs.length);
                int count = 0;
                for (int i : btnNums) {
                    edit.putInt("IntValue_" + count++, i);
                }
            }
            edit.commit();
        }


        int[] ret;
        Integer count1 = prefs.getInt("Count", 0);
        if (count1 != null) {
            ret = new int[count1];
            for (int i = 0; i < count1; i++) {
                ret[i] = prefs.getInt("IntValue_" + i, i);
                t = ret;
            }
        }


        if (t != null) {


            adapter = new MyAdapter(this, exp, images, mean, t);

        } else

        {
            adapter = new MyAdapter(this, exp, images, mean, btnNums);

        }


        list.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        context = getApplicationContext();
    }

}
class  MyAdapter  extends ArrayAdapter<String>{
    int [] images;
    String [] titleArray;
    String [] descriptionArray;
    int [] btnNums;
    MainActivity ma;










    public MyAdapter(MainActivity m, String[] titles, int imgs[], String[] descp, int[] btnNum ){

        super(m, R.layout.single_row, R.id.textView,titles);
        this.images=imgs;
        this.titleArray=titles;
        this.descriptionArray=descp;
        btnNums=btnNum;


        this.ma=m;







    }


    @Override
    public View getView(final int position, final View convertView, ViewGroup parent) {




        LayoutInflater inflater= (LayoutInflater) ma.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View row=inflater.inflate(R.layout.single_row, parent, false);

        final TextView myTitle=(TextView)row.findViewById(R.id.textView);

        myTitle.findViewById(R.id.textView);

        final TextView descp=(TextView)row.findViewById(R.id.textView2);
        final ImageView imageView = (ImageView) row.findViewById(R.id.imageView);
        final Button button = (Button) row.findViewById(R.id.angry_btn);
        final Vibrator a = (Vibrator) ma.getSystemService(Context.VIBRATOR_SERVICE);







        if(ma.t!=null)
        {


            for(int i=0; i<99;i++){
                if (btnNums[position] <= 0) {

                    button.setText("OK");







                 /*   Integer index=(Integer) convertView.getTag();
                  ma.list.removeViewAt(index);
                    ma.adapter.notifyDataSetChanged();
                    */

                }else {
                    button.setText("" + ma.t[position]);

                    imageView.setImageResource(images[position]);
                    myTitle.setText(titleArray[position]);
                    descp.setText(descriptionArray[position]);


                }



            }
        }else
        {

            for(int i=0; i<99;i++){

                if (btnNums[position] <= 0) {

                    button.setText("OK");
                    a.cancel();

                 /*   Integer index=(Integer) convertView.getTag();
                  ma.list.removeViewAt(index);
                    ma.adapter.notifyDataSetChanged();
                    */

                }else {
                    button.setText("" + btnNums[position]);


                }
                // button.setText("" + btnNums[position]);
       //error occurs here         imageView.setImageResource(images[position]);
                myTitle.setText(titleArray[position]);
                descp.setText(descriptionArray[position]);


            }
        }



        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                btnNums[position]--;

                storeIntArray(btnNums);


                a.vibrate(30);





                int temp[]=getFromPrefs();
                ma.t=temp;

                for (int i = 0; i <99; i++) {
                    if (btnNums[position] <= 0) {

                        button.setText("OK");
                        a.cancel();


                 /*   Integer index=(Integer) convertView.getTag();
                  ma.list.removeViewAt(index);
                    ma.adapter.notifyDataSetChanged();
                    */

                    }else {
                        button.setText("" + temp[position]);


                    }

                    imageView.setImageResource(images[position]);
                    myTitle.setText(titleArray[position]);
                    descp.setText(descriptionArray[position]);


                }
            }
        });




        return row;



    }



    @Override
    public long getItemId(int position) {

        return super.getItemId(position);
    }


    public  void storeIntArray(int[] array){

        ma.edit.putInt("Count", array.length);
        int count = 0;
        for (int i: array){
            ma.edit.putInt("IntValue_" + count++, i);
        }
        ma.edit.commit();
    }
    public int[] getFromPrefs(){

        int[] ret;


        int count = ma.prefs.getInt("Count", 0);
        ret = new int[count];
        for (int i = 0; i < count; i++){
            ret[i] =ma.prefs.getInt("IntValue_"+ i, i);
        }
        return ret;
    }

}

2 个答案:

答案 0 :(得分:0)

长度= 100的数组最多只能访问[99]。

因此,如果您正在尝试访问MyArray [100],那就超出了界限。这将是数组中第101个项目,因为数组基于零。

答案 1 :(得分:0)

检查适配器中指定的getCount()方法中的计数值。