1

首先,我是一个android新手,所以我的解决方法可能会很尴尬,我愿意接受建议。我正在尝试创建一个游戏管理器对象来处理活动之间的所有转换。我的目的是,在活动中,menuOut 方法将调用 GameManager 对象的 changeActivity 方法,并使用 nextActivity 参数,而 changeActivity 将启动该活动。我不断收到错误,但没有找到解决方案。

这是我的源代码: GameManager:

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 上使用两种方法:

  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构造函数并在类中保留一个静态实例GameManager(这是 Android 文档推荐的方式,但我个人更喜欢在逻辑上绑定到应用程序的第一种方式)。


此外,如果您仅使用中心类来执行静态操作,则可以将其所有方法标记为static并直接访问它们。将Context对象作为参数传递给这些方法,您应该能够在没有任何静态变量的情况下启动活动(有时很难在 Android 中正确实现,因为您的 VM 可能会不时重启)。

于 2013-08-19T00:30:07.133 回答