【问题标题】:Changing Activities in Java (Android Studio) without extending Activity在不扩展 Activity 的情况下更改 Java (Android Studio) 中的 Activity
【发布时间】:2018-09-28 19:54:15
【问题描述】:

大家好,我对 Java 比较陌生,我正在 Android Studio 中制作游戏。我遇到的问题是,当用户达到某个分数时,我试图切换级别。 Game 类本身扩展了 SurfaceView 类,而菜单本身扩展了 Activity。

我的主菜单类扩展了 Activity,并且我已经对其进行了编程,以便在用户单击按钮 (Play Game) 时运行游戏,但是,我尝试复制和粘贴代码,但它似乎不起作用在 Game 类中。

class SnakeEngine extends SurfaceView implements Runnable {
    public void update() {
     if (score == 5){
        Intent intent = new Intent(this, level2.class);
        startActivity(intent);
    }
    }

任何形式的帮助都将不胜感激。

谢谢。

【问题讨论】:

  • 尝试使用Intent intent = new Intent(getContext(), level2.class);getContext().startActivity(intent);
  • 我试过了,我得到错误“无法解析方法'startActivity'”

标签: java android android-studio android-activity


【解决方案1】:

SurfaceView 扩展自 View 类,该类具有 getContext() 方法,因此您可以像使用它一样

class SnakeEngine extends SurfaceView implements Runnable {
    public void update() {
     if (score == 5){
        Intent intent = new Intent(this, level2.class);
        getContext().startActivity(intent);
    }
}

【讨论】:

    猜你喜欢
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 2021-10-22
    • 2014-08-09
    相关资源
    最近更新 更多