检索数组列表的共享首选项时会抛出java.lang.IndexOutOfBoundsException

时间:2015-08-28 07:52:50

标签: java android sharedpreferences android-alertdialog

我可以将alertdialog setmultichoiceitems的值存储在共享首选项arraylist中。当我找回相同的我 -

  

java.lang.IndexOutOfBoundsException:索引0无效,大小为0

请帮忙。

final CharSequence[] ratings = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
    final ArrayList selectedratings = new ArrayList();
    final boolean[] ratingschecked = {false, false, false, false, false, false, false, false, false, false};
    SharedPreferences sharedPreferences = getSharedPreferences("checkedrate", Context.MODE_PRIVATE);
    final SharedPreferences.Editor editor = sharedPreferences.edit();

    final AlertDialog.Builder builder = new AlertDialog.Builder(this);

    int size = sharedPreferences.getInt("size", 0);
    for(int j=0;j<size;j++)
    {
        selectedratings.add(sharedPreferences.getString("selectedratings" + j, String.valueOf(selectedratings.get(j))));
    }


    for(int j=0;j<selectedratings.size();j++){
            int s = (int) selectedratings.get(j);
            ratingschecked[s-1]=true;
    }

    builder.setTitle("Select Ratings");
    builder.setMultiChoiceItems(ratings, ratingschecked, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            if (isChecked) {
                if(!selectedratings.contains((String)String.valueOf(which+1))){
                    selectedratings.add(String.valueOf(which+1));
                    ratingschecked[which+1]=true;
                }
            } else if (selectedratings.contains(which + 1)) {
                selectedratings.remove((String)String.valueOf(which));
                ratingschecked[which]=false;

            }
        }
    }).setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            // editor.putString("checked", String.valueOf(selectedratings));
            for (int i = 0; i < selectedratings.size(); i++) {
                editor.putString("selectedratings" + i, String.valueOf(selectedratings.get(i)));
            }
            editor.putInt("size", selectedratings.size());
            editor.apply();
            Log.e("Shiva", String.valueOf(selectedratings));


        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    AlertDialog dialog = builder.create();
    builder.show();
}

logcat的:

 08-28 13:28:50.668    7301-7301/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.example.snatarajan.allmovieratings, PID: 7301
        java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
                at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                at java.util.ArrayList.get(ArrayList.java:308)
                at com.example.snatarajan.allmovieratings.AllMovieRating.imdblist(AllMovieRating.java:62)
                at com.example.snatarajan.allmovieratings.AllMovieRating.onClick(AllMovieRating.java:38)
                at android.view.View.performClick(View.java:4780)
                at android.view.View$PerformClick.run(View.java:19866)
                at android.os.Handler.handleCallback(Handler.java:739)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5254)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    08-28 13:28:50.670      554-907/? W/ActivityManager﹕ Force finishing activity 1 com.example.snatarajan.allmovieratings/.AllMovieRating
    08-28 13:28:50.748    554-25538/? I/OpenGLRenderer﹕ Initialized EGL, version 1.4
    08-28 13:28:51.198      554-576/? W/ActivityManager﹕ Activity pause timeout for ActivityRecord{264c5519 u0 com.example.snatarajan.allmovieratings/.AllMovieRating t72 f}
    08-28 13:28:51.647      554-576/? I/ActivityManager﹕ Killing 5859:com.google.android.apps.magazines/u0a38 (adj 15): empty #17
    08-28 13:28:51.857      554-576/? E/libprocessgroup﹕ failed to kill 1 processes for processgroup 5859
    08-28 13:28:52.208      707-707/? W/ResourceType﹕ No package identifier when getting value for resource number 0x00000000
    08-28 13:28:52.209      707-707/? W/PackageManager﹕ Failure retrieving resources for com.example.snatarajan.allmovieratings: Resource ID #0x0
    08-28 13:29:01.861      554-576/? W/ActivityManager﹕ Activity destroy timeout for ActivityRecord{264c5519 u0 com.example.snatarajan.allmovieratings/.AllMovieRat

解决:

更改后

selectedratings.add(sharedPreferences.getString("selectedratings" + j, String.valueOf(selectedratings.get(j))));

selectedratings.add(sharedPreferences.getString("selectedratings" + j, null));

0 个答案:

没有答案