为什么我使用systemprefs使数组超出界限?

时间:2016-11-15 15:58:14

标签: android arrays system-preferences

我在Google Play上收到了我的应用有的错误报告:

java.lang.ArrayIndexOutOfBoundsException: length=0; index=9
    at punjokes.tuxstudios.com.punjokes.Pun.init(Pun.java:590)
    at punjokes.tuxstudios.com.punjokes.Pun.onCreateOptionsMenu(Pun.java:115)

我正在尝试调试应用,但我没有在Android Studio中收到错误。有什么我想念的吗?谢谢。我认为它与收藏夹有关。我有一个双关语列表和一个用户可以为他们的收藏夹添加书签的选项。

public class Pun extends AppCompatActivity {

    private InterstitialAd interstitial;

    public static final String mPath = "body.txt";
    public static final String mBusinessPath = "business.txt";
    public static final String mCrimePath = "crime.txt";
    public static final String mEducationPath = "education.txt";
    public static final String mEntertaintmentPath = "entertainment.txt";
    public static final String mFoodPath = "food.txt";
    public static final String mHealthPath = "health.txt";
    public static final String mNaturePath = "nature.txt";
    public static final String mPeoplePath = "people.txt";
    public static final String mPlacesPath = "places.txt";
    public static final String mTechnologyPath = "technology.txt";
    public static final String mTransportationPath = "transportation.txt";
    public static final String mWorkPath = "work.txt";
    private QuoteBank mQuoteBank;
    private List<String> mLines;
    private int counter = 0;
    private static int SIZE = 24;
    private String favorites1[] = new String[SIZE];
    private String favorites2[] = new String[SIZE];
    private String favorites3[] = new String[SIZE];
    private String favorites4[] = new String[SIZE];
    private String favorites5[] = new String[SIZE];
    private String favorites6[] = new String[SIZE];
    private String favorites7[] = new String[SIZE];
    private String favorites8[] = new String[SIZE];
    private String favorites9[] = new String[SIZE];
    private String favorites10[] = new String[SIZE];
    private String favorites11[] = new String[SIZE];
    private String favorites12[] = new String[SIZE];
    private String favorites13[] = new String[SIZE];

    public MenuItem star;

    public static int i1, i2;

    private ShareActionProvider shareAction;

    private TextView textView;
    private boolean listedPuns[] = new boolean[SIZE];
    private ArrayList<Integer> punShuffle;
    private int counter2 = 0;
    private int counter3 = 0;


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.pun, menu);
        star = menu.findItem(R.id.action_favoritestar);
        init();

        MenuItem shareItem = menu.findItem(R.id.menu_item_share);

        shareAction = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(intent.EXTRA_TEXT, "\"" + mLines.get(i1) + "\"\n\nSent from Pun Jokes: Funny Puns by Tux Studios\nhttps://play.google.com/store/apps/details?id=punjokes.tuxstudios.com.punjokes&hl=en");

        shareAction.setShareIntent(intent);

        return true;
    }



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pun);


        Toolbar myToolbar3 = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar3);


        for (int i2 = 0; i2 < SIZE - 1; i2++) {
            listedPuns[i2] = false;
        }

        // Create the interstitial.
        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("ca-app-pub-9704824959875003/5950200773");

        interstitial.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                requestNewInterstitial();
            }
        });

        requestNewInterstitial();

        mQuoteBank = new QuoteBank(this);

        textView = (TextView) findViewById(R.id.textView);


        Random r = new Random();
        i1 = r.nextInt(SIZE);

        mQuoteBank = new QuoteBank(this);
        mLines = mQuoteBank.readLine(mPath);

        textView.setText(mLines.get(i1));

        favorites1 = new String[SIZE];
        favorites2 = new String[SIZE];
        favorites3 = new String[SIZE];
        favorites4 = new String[SIZE];
        favorites5 = new String[SIZE];
        favorites6 = new String[SIZE];
        favorites7 = new String[SIZE];
        favorites8 = new String[SIZE];
        favorites9 = new String[SIZE];
        favorites10 = new String[SIZE];
        favorites11 = new String[SIZE];
        favorites12 = new String[SIZE];
        favorites13 = new String[SIZE];

        // Save and Load array
        favorites1 = loadArray("favorites1", this);
        favorites2 = loadArray("favorites2", this);
        favorites3 = loadArray("favorites3", this);
        favorites4 = loadArray("favorites4", this);
        favorites5 = loadArray("favorites5", this);
        favorites6 = loadArray("favorites6", this);
        favorites7 = loadArray("favorites7", this);
        favorites8 = loadArray("favorites8", this);
        favorites9 = loadArray("favorites9", this);
        favorites10 = loadArray("favorites10", this);
        favorites11 = loadArray("favorites11", this);
        favorites12 = loadArray("favorites12", this);
        favorites13 = loadArray("favorites13", this);



        final Button btn1 = (Button) findViewById(R.id.next);
        btn1.setOnClickListener(new View.OnClickListener() {
                                    public void onClick(View v) {
                                        star.setIcon(R.drawable.starblank);

                                        i1 = randomExcluded();

                                        textView.setText(mLines.get(i1));
                                        invalidateOptionsMenu();
                                        counter++;

                                        counter3++;

                                        if (counter == 3) {
                                            Random r2 = new Random();
                                            i2 = r2.nextInt(11) + 1;

                                            Log.d("i2", "" + i2);
                                            counter = 0;

                                            if (i2 > 5) {
                                                if (interstitial.isLoaded())
                                                    interstitial.show();
                                            }
                                        }

                                        if(counter3 == 10){
                                            Random r2 = new Random();
                                            i2 = r2.nextInt(11) + 1;

                                            counter = 0;

                                        }


                                        switch (MainActivity.category) {
                                            case "body":
                                                if (favorites1[i1].equals("true")) {
                                                    star.setIcon(R.drawable.staryellow);
                                                } else {
                                                    star.setIcon(R.drawable.starblank);
                                                }
                                                break;
                                            case "crime":
                                                if (favorites2[i1].equals("true")) {
                                                    star.setIcon(R.drawable.starblank);
                                                } else {
                                                    star.setIcon(R.drawable.staryellow);
                                                }
                                                break;
                                            case "entertainment":
                                                if (favorites3[i1].equals("true")) {
                                                    star.setIcon(R.drawable.staryellow);
                                                } else {
                                                    star.setIcon(R.drawable.starblank);
                                                }
                                                break;
                                            case "health":
                                                if (favorites4[i1].equals("true")) {
                                                    star.setIcon(R.drawable.staryellow);
                                                } else {
                                                    star.setIcon(R.drawable.starblank);
                                                }
                                                break;
                                            case "people":
                                                if (favorites5[i1].equals("true")) {
                                                    star.setIcon(R.drawable.staryellow);
                                                } else {
                                                    star.setIcon(R.drawable.starblank);
                                                }
                                                break;
                                            case "technology":
                                                if (favorites6[i1].equals("true")) {
                                                    star.setIcon(R.drawable.staryellow);
                                                } else {
                                                    star.setIcon(R.drawable.starblank);
                                                }
                                                break;
                                            case "work":
                                                if (favorites7[i1].equals("true")) {
                                                    star.setIcon(R.drawable.staryellow);
                                                } else {
                                                    star.setIcon(R.drawable.starblank);
                                                }
                                                break;
                                            case "business":
                                                if (favorites8[i1].equals("true")) {
                                                    star.setIcon(R.drawable.staryellow);
                                                } else {
                                                    star.setIcon(R.drawable.starblank);
                                                }
                                                break;
                                            case "education":
                                                if (favorites9[i1].equals("true")) {
                                                    star.setIcon(R.drawable.staryellow);
                                                } else {
                                                    star.setIcon(R.drawable.starblank);
                                                }
                                                break;
                                            case "food":
                                                if (favorites10[i1].equals("true")) {
                                                    star.setIcon(R.drawable.staryellow);
                                                } else {
                                                    star.setIcon(R.drawable.starblank);
                                                }
                                                break;
                                            case "nature":
                                                if (favorites11[i1].equals("true")) {
                                                    star.setIcon(R.drawable.staryellow);
                                                } else {
                                                    star.setIcon(R.drawable.starblank);
                                                }
                                                break;
                                            case "places":
                                                if (favorites12[i1].equals("true")) {
                                                    star.setIcon(R.drawable.staryellow);
                                                } else {
                                                    star.setIcon(R.drawable.starblank);
                                                }
                                                break;
                                            case "transportation":
                                                if (favorites13[i1].equals("true")) {
                                                    star.setIcon(R.drawable.staryellow);
                                                } else {
                                                    star.setIcon(R.drawable.starblank);
                                                }
                                                break;
                                            default:
                                                break;
                                        }

                                        saveArray(favorites1, "favorites1", Pun.this);
                                        saveArray(favorites2, "favorites2", Pun.this);
                                        saveArray(favorites3, "favorites3", Pun.this);
                                        saveArray(favorites4, "favorites4", Pun.this);
                                        saveArray(favorites5, "favorites5", Pun.this);
                                        saveArray(favorites6, "favorites6", Pun.this);
                                        saveArray(favorites7, "favorites7", Pun.this);
                                        saveArray(favorites8, "favorites8", Pun.this);
                                        saveArray(favorites9, "favorites9", Pun.this);
                                        saveArray(favorites10, "favorites10", Pun.this);
                                        saveArray(favorites11, "favorites11", Pun.this);
                                        saveArray(favorites12, "favorites12", Pun.this);
                                        saveArray(favorites13, "favorites13", Pun.this);
                                    }
                                }

        );


    }


    private void requestNewInterstitial() {
        AdRequest adRequest = new AdRequest.Builder()
                .build();

        interstitial.loadAd(adRequest);
    }

    public boolean saveArray(String[] array, String arrayName, Context mContext) {
        SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(arrayName + "_size", array.length);
        for (int i = 0; i < array.length; i++)
            editor.putString(arrayName + "_" + i, array[i]);


        return editor.commit();
    }

    public String[] loadArray(String arrayName, Context mContext) {
        SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);
        int size = prefs.getInt(arrayName + "_size", 0);
        String array[] = new String[size];
        for (int i = 0; i < size; i++) {
            array[i] = prefs.getString(arrayName + "_" + i, null);
            //if (array[i] == null)
        }
        return array;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.about:
                // User chose the "Favorite" action, mark the current item
                // as a favorite...
                final Intent intent = new Intent(Pun.this, About.class);
                startActivity(intent);

                return true;

            case R.id.upgrade:
                alertDialog();
                return true;


            case R.id.submit:
                // User chose the "Favorite" action, mark the current item
                // as a favorite...
                final Intent intent1 = new Intent(Pun.this, Submit.class);
                startActivity(intent1);
                return true;

            case R.id.action_favorite:
                final Intent intent2 = new Intent(Pun.this, Favorites.class);
                startActivity(intent2);

                return true;

            case R.id.action_favoritestar:

                switch (MainActivity.category) {
                    case "body":
                        if (favorites1[i1].equals("true")) {
                            favorites1[i1] = "false";
                            star.setIcon(R.drawable.starblank);
                        } else {
                            favorites1[i1] = "true";
                            star.setIcon(R.drawable.staryellow);
                        }

                        Log.d("myTag", " " + favorites1[i1]);
                        break;
                    case "crime":
                        if (favorites2[i1].equals("true")) {
                            favorites2[i1] = "false";
                            star.setIcon(R.drawable.starblank);
                        } else {
                            favorites2[i1] = "true";
                            star.setIcon(R.drawable.staryellow);
                        }

                        Log.d("myTag", " " + favorites2[i1]);
                        break;
                    case "entertainment":
                        if (favorites3[i1].equals("true")) {
                            favorites3[i1] = "false";
                            star.setIcon(R.drawable.starblank);
                        } else {
                            favorites3[i1] = "true";
                            star.setIcon(R.drawable.staryellow);
                        }
                        break;
                    case "health":
                        if (favorites4[i1].equals("true")) {
                            favorites4[i1] = "false";
                            star.setIcon(R.drawable.starblank);
                        } else {
                            favorites4[i1] = "true";
                            star.setIcon(R.drawable.staryellow);
                        }
                        break;
                    case "people":
                        if (favorites5[i1].equals("true")) {
                            favorites5[i1] = "false";
                            star.setIcon(R.drawable.starblank);
                        } else {
                            favorites5[i1] = "true";
                            star.setIcon(R.drawable.staryellow);
                        }
                        break;
                    case "technology":
                        if (favorites6[i1].equals("true")) {
                            favorites6[i1] = "false";
                            star.setIcon(R.drawable.starblank);
                        } else {
                            favorites6[i1] = "true";
                            star.setIcon(R.drawable.staryellow);
                        }
                        break;
                    case "work":
                        if (favorites7[i1].equals("true")) {
                            favorites7[i1] = "false";
                            star.setIcon(R.drawable.starblank);
                        } else {
                            favorites7[i1] = "true";
                            star.setIcon(R.drawable.staryellow);
                        }
                        break;
                    case "business":
                        if (favorites8[i1].equals("true")) {
                            favorites8[i1] = "false";
                            star.setIcon(R.drawable.starblank);
                        } else {
                            favorites8[i1] = "true";
                            star.setIcon(R.drawable.staryellow);
                        }
                        break;
                    case "education":
                        if (favorites9[i1].equals("true")) {
                            favorites9[i1] = "false";
                            star.setIcon(R.drawable.starblank);
                        } else {
                            favorites9[i1] = "true";
                            star.setIcon(R.drawable.staryellow);
                        }
                        break;
                    case "food":
                        if (favorites10[i1].equals("true")) {
                            favorites10[i1] = "false";
                            star.setIcon(R.drawable.starblank);
                        } else {
                            favorites10[i1] = "true";
                            star.setIcon(R.drawable.staryellow);
                        }
                        break;
                    case "nature":
                        if (favorites11[i1].equals("true")) {
                            favorites11[i1] = "false";
                            star.setIcon(R.drawable.starblank);
                        } else {
                            favorites11[i1] = "true";
                            star.setIcon(R.drawable.staryellow);
                        }
                        break;
                    case "places":
                        if (favorites12[i1].equals("true")) {
                            favorites12[i1] = "false";
                            star.setIcon(R.drawable.starblank);
                        } else {
                            favorites12[i1] = "true";
                            star.setIcon(R.drawable.staryellow);
                        }
                        break;
                    case "transportation":
                        if (favorites13[i1].equals("true")) {
                            favorites13[i1] = "false";
                            star.setIcon(R.drawable.starblank);
                        } else {
                            favorites13[i1] = "true";
                            star.setIcon(R.drawable.staryellow);
                        }
                        Log.d("myTag", " " + favorites13[i1]);
                        break;
                    default:
                        break;



                }


                saveArray(favorites1, "favorites1", Pun.this);
                saveArray(favorites2, "favorites2", Pun.this);
                saveArray(favorites3, "favorites3", Pun.this);
                saveArray(favorites4, "favorites4", Pun.this);
                saveArray(favorites5, "favorites5", Pun.this);
                saveArray(favorites6, "favorites6", Pun.this);
                saveArray(favorites7, "favorites7", Pun.this);
                saveArray(favorites8, "favorites8", Pun.this);
                saveArray(favorites9, "favorites9", Pun.this);
                saveArray(favorites10, "favorites10", Pun.this);
                saveArray(favorites11, "favorites12", Pun.this);
                saveArray(favorites12, "favorites13", Pun.this);
                saveArray(favorites13, "favorites13", Pun.this);

                return true;

            case R.id.menu_item_share:


                return true;

            default:
                // If we got here, the user's action was not recognized.
                // Invoke the superclass to handle it.
                return super.onOptionsItemSelected(item);

        }
    }

    public void init() {
        Log.d("myTag", " " + i1);

        switch (MainActivity.category) {
            case "body":
                mQuoteBank = new QuoteBank(this);
                mLines = mQuoteBank.readLine(mPath);


                if (favorites1[i1].equals("true"))
                    star.setIcon(R.drawable.staryellow);
                break;
            //[...] same as above but for favorites2, 3, etc.
        }
    }

    public int randomExcluded() {
        if(counter2 == 0){
            counter2++;
            punShuffle = new ArrayList<>();
            for (int i = 1; i <= SIZE; ++i) punShuffle.add(i);
            Collections.shuffle(punShuffle);
        }else if(counter2 == SIZE-1) {
            counter2 = 0;
        } else{
            counter2++;
        }

        Log.d("counter", "" + counter2);

        return punShuffle.get(counter2) - 1;

    }




}

0 个答案:

没有答案