Android/Java:从类方法启动活动

发布于 2024-12-09 01:55:47 字数 1685 浏览 0 评论 0原文

我正在尝试从非活动课程开始一项新活动。

从主菜单:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class Menu extends Activity {


    Button start, options;
    GameLoop Game = new GameLoop();

    @Override

    public void onCreate(Bundle mainStart) {
        super.onCreate(mainStart);
        setContentView(R.layout.menu);

    start = (Button) findViewById(R.id.bStart);

    options = (Button) findViewById(R.id.bOptions);

    start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent openStart = new Intent(Menu.this, Game.class);
            startActivity(openStart);

        }
    });

    options.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Context mContext = null; //Error called for mContext to be initialized so just tried setting to null. This is most likely the error cause it would make more sense for it to be equal to "getContext()" or something like that
            Game.Start(mContext);//Here
        }
    });

    }

}

我正在尝试从 Game.Start() 方法打开一个活动。

import android.content.Context;
import android.content.Intent;

public class GameLoop extends Menu{
    boolean hello = false;

    public void Start(Context sContext){
        Intent openOptions = new Intent(sContext, Options.class);
        startActivity(openOptions);

    }

}

我不确定使用上下文是否是解决此问题的正确方法,但我认为值得一试。我对 java 和 android 完全陌生,所以我几乎不知道下一步该去哪里。任何关于采取什么方向的帮助将不胜感激。

I'm trying to start a new activity from a non-activity class.

From the main menu:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class Menu extends Activity {


    Button start, options;
    GameLoop Game = new GameLoop();

    @Override

    public void onCreate(Bundle mainStart) {
        super.onCreate(mainStart);
        setContentView(R.layout.menu);

    start = (Button) findViewById(R.id.bStart);

    options = (Button) findViewById(R.id.bOptions);

    start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent openStart = new Intent(Menu.this, Game.class);
            startActivity(openStart);

        }
    });

    options.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Context mContext = null; //Error called for mContext to be initialized so just tried setting to null. This is most likely the error cause it would make more sense for it to be equal to "getContext()" or something like that
            Game.Start(mContext);//Here
        }
    });

    }

}

I'm trying to open an activity from the Game.Start() method.

import android.content.Context;
import android.content.Intent;

public class GameLoop extends Menu{
    boolean hello = false;

    public void Start(Context sContext){
        Intent openOptions = new Intent(sContext, Options.class);
        startActivity(openOptions);

    }

}

I'm not sure if using context would be the right way of going about this but I figured it was worth a try. Im entirely new to java and android so I'm pretty much lost on where to go next. Any help in what direction to take would be throughly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妥活 2024-12-16 01:55:47

Activity 扩展了 Context,因此您可以在 Activity 内部使用 this

Game.Start(Menu.this);

我使用 Menu.this 因为您位于内部匿名类 (View.OnClickListener) 中,其中 this 引用此内部类。

Activity extends Context, so you can just use this when inside Activity.

Game.Start(Menu.this);

I use Menu.this because you are inside inner anonymous class (View.OnClickListener) where this refers to this inner class.

永不分离 2024-12-16 01:55:47

您是否将新活动添加到 androidmanifest.xml 中?

Do you added the new activities to the androidmanifest.xml?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文