首先,我是一个android菜鸟,所以我的解决方法可能会让人觉得尴尬,我愿意接受建议。 我正在尝试创建一个游戏管理器对象来处理 Activity 之间的所有转换。我的目的是在 Activity 中,menuOut 方法将使用 nextActivity 参数调用 GameManager 对象的 changeActivity 方法,而 changeActivity 将启动该 Activity 。我一直收到错误,但没有找到解决方案。
这是我的源代码: 游戏管理器:
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 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 方法。 我很累得到空指针异常。 有什么想法吗?
最佳答案
据我了解,您正在尝试实现单例模式。我建议在 Android 上使用两种方法:
扩展
Application
类,在 list 中注册您的类并使用getApplication()
在您的 Activity 中访问它:// 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 文档推荐的方式,但我个人更喜欢第一种方式绑定(bind)到应用程序)。
另外,如果你只使用你的中心类来做静态的事情,你可以将它的所有方法标记为 static
并直接访问它们。将 Context
对象作为参数传递给这些方法,您应该能够在没有任何静态变量的情况下启动 Activity (有时很难在 Android 中正确实现,因为您的 VM 可能会不时重新启动) .
关于Android - 从 Activity 调用普通对象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18304957/