0

Activity を拡張する抽象 CycleActivity があります。CycleActivity を拡張する他のアクティビティもいくつかあります。アクティビティの 1 つが作成されると、次を受け取ります。

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;
}
}

ゲームアクティビティ.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 に答える