0

I have an abstract CycleActivity which extends Activity. I have also a few other activities which extend CycleActivity. When one of the activities is created I receive:

07-28 19:42:20.850: E/AndroidRuntime(21162): FATAL EXCEPTION: main
07-28 19:42:20.850: E/AndroidRuntime(21162): java.lang.StackOverflowError
07-28 19:42:20.850: E/AndroidRuntime(21162):    at gospo.plaga.GameActivity.onCreate(GameActivity.java:23)
07-28 19:42:20.850: E/AndroidRuntime(21162):    at gospo.plaga.CycleActivity.onCreate(CycleActivity.java:29)
07-28 19:42:20.850: E/AndroidRuntime(21162):    at gospo.plaga.GameActivity.onCreate(GameActivity.java:23)
07-28 19:42:20.850: E/AndroidRuntime(21162):    at gospo.plaga.CycleActivity.onCreate(CycleActivity.java:29)
07-28 19:42:20.850: E/AndroidRuntime(21162):    at gospo.plaga.GameActivity.onCreate(GameActivity.java:23)

What is the reason for this behaviour?

CycleActivity.java

package gospo.plaga;

import gospo.plaga.game.Game;
import gospo.plaga.game.GameState;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;

public abstract class CycleActivity extends Activity
{
private GameState finishAfterGameState;
private Class<?> nextActivityClass;
private Timer gameStateChangeHandler;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}

protected void onCreate(Bundle savedInstanceState,
        GameState finishAfterGameState, Class<?> nextActivityClass)
{
    onCreate(savedInstanceState);
    this.finishAfterGameState = finishAfterGameState;
    this.nextActivityClass = nextActivityClass;

    gameStateChangeHandler = new Timer();
    TimerTask gameStateChangeHandlerTask = new TimerTask()
    {
        @Override
        public void run()
        {
            handleGameStateChange();
        }
    };
    final int GameStateChangeHandlePeriod = 1000;// TODO:
    gameStateChangeHandler.schedule(gameStateChangeHandlerTask, 0,
            GameStateChangeHandlePeriod);

}

private void handleGameStateChange()
{
    Game game = Game.getInstance();

    long finishesAt = 0;

    GameState[] gameStates = GameState.values();
    for (GameState gameState : gameStates)
    {
        finishesAt += gameState.getDuration();
        if (gameState.equals(finishAfterGameState))
        {
            break;
        }
    }

    if (game.getGameTime() > finishesAt)
    {
        gameStateChangeHandler.cancel();

        runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {
                Intent intent = new Intent(getApplicationContext(),
                        nextActivityClass);
                startActivity(intent);
                finish();
            }
        });
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.result, menu);
    return true;
}
}

GameActivity.java

...
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState, GameState.Tour, ResultActivity.class);
...
4

1 回答 1

3

你有一个循环引用的事情发生在这里。您的 CycleActivity 会创建一个 GameActivity ,反之亦然,因此由于无限递归,您会耗尽堆栈。

于 2013-07-28T18:03:14.193 回答