创建继承的Activity时的StackOverFlowError

时间:2013-07-28 18:01:40

标签: java android android-activity

我有一个抽象的CycleActivity,它扩展了Activity。我还有一些其他活动可以扩展CycleActivity。 当我创建其中一个活动时,我会收到:

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)

这种行为的原因是什么?

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);
...

1 个答案:

答案 0 :(得分:3)

你在这里有一个循环引用的东西。您的CycleActivity会创建GameActivity,反之亦然,因此无限递归会导致堆栈耗尽。