1

まず、私は Android の新人なので、私の解決方法はぎこちなく感じるかもしれませんが、私は提案を受け入れます。アクティビティ間のすべての遷移を処理するゲーム マネージャー オブジェクトを作成しようとしています。そして、私の目的は、アクティビティ中に menuOut メソッドが GameManager オブジェクトの changeActivity メソッドを nextActivity 引数で呼び出し、changeActivity がそのアクティビティを開始することです。一貫してエラーが発生し、解決策が見つかりませんでした。

ここに私のソースコードがあります:

public class GameManager{
    public SplashScreen splash = new SplashScreen();
    public MainScreen main = new MainScreen();
    public LoadingScreen load = new LoadingScreen();

    Context tempContext;

    public GameManager(Context base) {
        super();
        tempContext = base;
    }

    public void start(){
        createScreens();
        Intent intent = new Intent(tempContext, splash.getClass());
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        tempContext.startActivity(intent);
    }

    public void createScreens() {
        //here is the method that i am trying to find a solution

        ((SplashScreen)splash.getContext()).setGameClass(this);
        ((MainScreen)main.getContext()).setGameClass(this);
        ((LoadingScreen)load.getContext()).setGameClass(this);
    }

    public void changeMenu(MenuType nextMenu, MenuType previousMenu){
        Intent intent2;
        switch(nextMenu){
        case MAIN_SC:
            tempContext = main.getContext();
            intent2.setClass(tempContext, main.getClass());
            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            tempContext.startActivity(intent2);
        case GAME_LOADING_SC:
            tempContext = load.getContext();
            intent2.setClass(tempContext, load.getClass());
            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            tempContext.startActivity(intent2);
        default:
            break;
        }
    }
}

SplashScreen のアクティビティは次のとおりです。

public class SplashScreen extends Activity {
    public Context context = this;
    public GameManager gameman;
    private static final int SPLASH_DURATION = 4000;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        splash();
        menuOut();
    }

    public Context getContext() {
        return this;
    }

    public void splash() {
         LinearLayout ll = new LinearLayout(this);
         ll.setOrientation(LinearLayout.HORIZONTAL);
         ll.setBackgroundResource(R.drawable.game_loop_splash);

         setContentView(ll);

         Handler handler = new Handler();

         // run a thread after 2 seconds to start the home screen
         handler.postDelayed(new Runnable() {
             @Override
             public void run() {
                 finish();
             }
         }, SPLASH_DURATION);
    }

    public void setGameClass(GameManager game){
        gameman = game;
    }

    private void menuOut(){
        gameman.changeMenu(MenuType.GAME_LOADING_SC, MenuType.GAME_SPLASH_SC);
        this.onDestroy();
    }
}

GameManager に戻って changeMenu メソッドを呼び出すことができません。私はヌルポインタ例外を取得することに非常に疲れています。何か案が?

4

1 に答える 1

1

私が読んだところによると、あなたはシングルトン パターンを実装しようとしています。Androidでこれを行うには、次の2つの方法をお勧めします。

  1. クラスを拡張し、Applicationクラスをマニフェストに登録getApplication()し、アクティビティで使用してアクセスします。

    // In MyApplicationSubclass.java:
    public final class MyApplicationSubclass extends Application {
      /* ... */
      public void myMethod() {
        // insert some code here
      }
      /* ... */
    }
    
    // From your Activity:
    ((MyApplicationSubclass) this.getApplication()).myMethod();
    
  2. 「通常の」Java シングルトン パターンを使用します。たとえば、privateコンストラクターを使用し、クラス内に 1 つの静的インスタンスを保持しますGameManager(これは Android のドキュメントで推奨されている方法ですが、アプリケーションに論理的にバインドされているものがある場合は、個人的には最初の方法を好みます)。


また、中心的なクラスを静的なものにのみ使用している場合は、そのすべてのメソッドをマークしstaticて直接アクセスできます。オブジェクトをパラメーターとしてこれらのメソッドに転送Contextすると、静的変数なしでアクティビティを開始できるはずです (VM が時々再起動される可能性があるため、Android で適切に実装するのが難しい場合があります)。

于 2013-08-19T00:30:07.133 に答える