2012-06-06 28 views
1

我的應用程序出現問題,我無法弄清楚問題所在。在ICS上,該應用程序可以工作,但在任何2.X上都有問題。我相信它與循環有關,但我仍然有點困惑。java.lang.StackOverflowError在我的應用程序中導致問題

開發者控制檯拋出這個在我的錯誤報告:

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

這是導致該問題的代碼...

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(); 
    } 
} 
+3

TLDR;您需要將其剝離回到我們查看問題所需的最低限度。應該相當容易,因爲你實際上得到了一個異常,而不是一個掛起或崩潰等。 – John3136

+0

TOO MUCH CODE - 你似乎沒有參考setScale在那裏的任何地方。 –

+0

我不會在任何地方使用setScale,這就是爲什麼我想知道那是指什麼。但現在一切都很好,謝謝你們的幫助! – SkateJerrySkate

回答

1

堆棧溢出的一個常見原因是大遞歸調用的數量 - 或者是由於導致無限遞歸的錯誤,或者是剛剛被構造爲具有非常深的遞歸的代碼。

在你的情況下,堆棧溢出可能是由於你的代碼具有相當大的遞歸調用深度。你能將遞歸調用重構爲while循環嗎?例如,而不是:

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

你能不能做到以下幾點:

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

這就是我要給出的答案,你的通話深度可能是366個電話到最後,這對於移動設備。 –

+0

非常感謝Happy Dave,這似乎解決了我的問題。我愛StackFlow。 – SkateJerrySkate

相關問題