Search code examples
javaandroidloopsstack-overflow

java.lang.StackOverflowError causing problems in my app


I am having a problem with my app, I cannot figure out what the problem is. On ICS the app works, but on anything 2.X it has problems. I believe it has to do with the loop but I am still a bit confused.

Developer console throws this at me for the error report:

Exception class > java.lang.StackOverflowError
Source method > Matrix.setScale()

This is the code that is causing the problem...

private void shiftLoop7()
{
    if (d7 != doy && d7 < 366)
    {
        d7 = d7 + 8;
        shiftLoop7();
    }
    else if(d7 == doy)
    {

        if (hour >= 0 && hour < 8)
        {
            shift.setText("C");
            shift.setTextAppearance(getApplicationContext(), R.style.CShift);

            shiftImage.setImageResource(R.drawable.c);
            timeTill.setText("till 7:45 AM");

            dayshift.setText("A Shift");
            day1.setBackgroundResource(R.color.A);
            day2.setBackgroundResource(R.color.A);
            day3.setBackgroundResource(R.color.A);
            day4.setBackgroundResource(R.color.A);

            nightshift.setText("C Shift");
            night1.setBackgroundResource(R.color.C);
            night2.setBackgroundResource(R.color.C);
            night3.setBackgroundResource(R.color.C);
            night4.setBackgroundResource(R.color.C);
        }
        else if (hour >= 8 && hour < 17)
        {
            shift.setText("A");
            shift.setTextAppearance(getApplicationContext(), R.style.AShift);

            shiftImage.setImageResource(R.drawable.a);
            timeTill.setText("till 4:45 PM");

            dayshift.setText("A Shift");
            day1.setBackgroundResource(R.color.A);
            day2.setBackgroundResource(R.color.A);
            day3.setBackgroundResource(R.color.A);
            day4.setBackgroundResource(R.color.A);

            nightshift.setText("C Shift");
            night1.setBackgroundResource(R.color.C);
            night2.setBackgroundResource(R.color.C);
            night3.setBackgroundResource(R.color.C);
            night4.setBackgroundResource(R.color.C);
        }
        else
        {
            shift.setText("C");
            shift.setTextAppearance(getApplicationContext(), R.style.CShift);

            shiftImage.setImageResource(R.drawable.c);
            timeTill.setText("till 7:45 AM");

            dayshift.setText("A Shift");
            day1.setBackgroundResource(R.color.A);
            day2.setBackgroundResource(R.color.A);
            day3.setBackgroundResource(R.color.A);
            day4.setBackgroundResource(R.color.A);

            nightshift.setText("C Shift");
            night1.setBackgroundResource(R.color.C);
            night2.setBackgroundResource(R.color.C);
            night3.setBackgroundResource(R.color.C);
            night4.setBackgroundResource(R.color.C);
        }
    }
    else
    {
        shiftLoop8();
    }
}

Solution

  • A common cause of stack overflow is a large number of recursive calls - either due to a bug causing infinite recursion, or code that is just structured to have very deep recursion.

    In your case, the stack overflow may be caused by the fact that your code has potential for a fairly large depth of recursive calls. Could you restructure the recursive calls to just be while loops? For example, instead of:

    if (d7 != doy && d7 < 366)
    {
        d7 = d7 + 8;
        shiftLoop7();
    }
    

    could you just do the following:

    while (d7 != doy && d7 < 366)
    {
        d7 = d7 + 8;
    }