Android - 从活动调用普通对象方法 [英] Android - Calling an ordinary object method from an activity

查看:21
本文介绍了Android - 从活动调用普通对象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是一个安卓新手,所以我的解决方法可能很尴尬,我愿意接受建议.我正在尝试创建一个处理活动之间所有转换的游戏管理器对象.我的目的是,在活动中,menuOut 方法将使用 nextActivity 参数调用 GameManager 对象的 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.

这是我的源代码:游戏经理:

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

这是 SplashScreen 活动:

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

我无法返回到 GameManager 并调用 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:

  1. 扩展Application 类,在清单中注册您的类并使用 getApplication() 在您的活动中访问它:

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

  • 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).

    <小时>

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


    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).

    这篇关于Android - 从活动调用普通对象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆