我有一个抽象的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);
...

最佳答案

您正在这里进行循环参考。您的CycleActivity创建一个GameActivity,反之亦然,因此由于无限递归,您的堆栈用完了。

关于java - 创建继承的Activity时出现StackOverFlowError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17911154/

10-12 05:52