Android的 - 调用从活动中一个普通的对象方法一个普通、对象、活动中、方法

2023-09-12 22:14:30 作者:往事通缉犯

首先,我是一个Android新秀,所以我的解决方案的方式,可以发现尴尬的,我愿意听取建议。 我试图创建一个处理活动之间的所有转变的游戏管理器对象。而我的目的是,当一个活动,menuOut方法调用游戏管理对象与nextActivity论点changeActivity方法和changeActivity将启动该活动。我收到错误持续,并没有找到一个解决方案。

First, I am an android rookie, so my solution ways can be found awkward, and i am open to suggestions. I am trying to create a game manager object that handles all transitions between activities. And my purpose is that while in an activity, menuOut method will call the changeActivity method of GameManager object with nextActivity argument and changeActivity will start that Activity. I am getting errors consistently, and did not find a solution.

下面是我的源$ C ​​$ CS: 游戏管理:

Here is my source codes: 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;
        }
    }
}

在这里是闪屏活动:

And here is SplashScreen activity:

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

我不能返回到游戏管理和调用changeMenu方法。 我很疲惫,以获得空指针异常。 你知道吗?

I can not return to the GameManager and call the changeMenu method. I am very exhausted to get null pointer exceptions. Any idea?

推荐答案

从我读,你想实现一个单例模式。有两种方法,我建议你这样做在Android上:

From what I read, you are trying to implement a singleton pattern. There are two ways I'd recommend to do that on android:

延长 应用程序 类,登记类的清单和使用的 getApplication() 你们的活动来访问它:

Extend the Application class, register your class in the manifest and use getApplication() in your activities to get access to it:

// In MyApplicationSubclass.java:
public final class MyApplicationSubclass extends Application {
  /* ... */
  public void myMethod() {
    // insert some code here
  }
  /* ... */
}

// From your Activity:
((MyApplicationSubclass) this.getApplication()).myMethod();

使用普通的java单例模式,例如:使用私人构造,并在你的游戏管理类保留一个静态实例(这​​是方式的Andr​​oid文档建议,但我个人preFER有东西在逻辑上绑定到应用程序)时,第一种方式。

当Android系统加载一个新Activity对象到当前屏幕时,旧Activity对象的哪个方法将会被最先触发

Use a "normal" java singleton pattern, e.g. use a private constructor and keep one static instance within your GameManager class (this is the way the Android docs recommend, but I personally prefer the first way when having something that is logically bound to the Application).

另外,如果你只使用你的核心类做静态的东西,你可以标记所有为静态法和直接访问它们。转让上下文对象作为参数传递给这些方法,你应该能够启动活动没有任何静态变量(有时是很难实现正常的机器人,为你的虚拟机可能会重新开始从时间到时间)。

Also, if you're only using your central class to do static stuff, you can just mark all its method as static and access them directly. Transfer Context objects as parameters to these methods, and you should be able to start activities without any static variables (which are sometimes hard to implement properly in Android, as your VM might get restarted from time to time).