在 Android 中管理两个 Activity 时出现问题

发布于 2024-12-06 07:10:45 字数 1436 浏览 0 评论 0原文

我想做的事情非常简单。我创建了两个类 A 和 B。我在 A 中创建了一个单击处理程序,它调用 B 中的函数,而 B 又调用 A 中的函数。在 AI 中的被调用函数中创建一个按钮。当我尝试按下按钮时,我的程序被强制关闭。

类日志

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class Loggs extends Activity {

    Model model;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void clickHandler(View v)
    {
        model = new Model();
        model.startGame();
        //click();
    }

    public void startGame()
    {
        Log.d("Log","Reached start game");
        click();
    }
    public void click()
    {
        Log.d("Log","Reached click");
        Button btn =(Button)findViewById(R.id.startButton);
        btn.setEnabled(false);
    }

}

类模型

import android.app.Activity; import android.os.Bundle; import android.util.Log;


public class Model extends Activity{
    Loggs log;  

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }   

    public void startGame() {
        log= new Loggs();       
        Log.d("Logg","Reached start game Model");       
        log.startGame();    
    }
}

What I am trying to do is very simple. I created two classes A and B. I created a click handler in A which calls a function in B which in turn calls a function in A. In the called function in A I am create a button. My programs is being forced close when I try to push the button.

Class Loggs

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class Loggs extends Activity {

    Model model;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void clickHandler(View v)
    {
        model = new Model();
        model.startGame();
        //click();
    }

    public void startGame()
    {
        Log.d("Log","Reached start game");
        click();
    }
    public void click()
    {
        Log.d("Log","Reached click");
        Button btn =(Button)findViewById(R.id.startButton);
        btn.setEnabled(false);
    }

}

Class Model

import android.app.Activity; import android.os.Bundle; import android.util.Log;


public class Model extends Activity{
    Loggs log;  

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }   

    public void startGame() {
        log= new Loggs();       
        Log.d("Logg","Reached start game Model");       
        log.startGame();    
    }
}

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

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

发布评论

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

评论(3

静谧 2024-12-13 07:10:45

R.id.startButton 是在 R.layout.main 中吗?比..你不能用new实例化一个活动,imo,因为活动的默认构造函数是私有的(我认为)。看一下意图

is R.id.startButton in R.layout.main? Than.. you cannot instantiate an activity with new, imo, because the default constructor for activity are private (i think). Take a look at intent

祁梦 2024-12-13 07:10:45

它不太清楚你想在这里做什么?活动不会以这种方式相互通信。如果您希望一个活动启动另一个活动,您需要使用意图来执行此操作:

例如,如果您想从 Loggs 进行模型活动,您将发出以下命令:

Intent i = new Intent(this, Model.class);
this.startActivity(i);

虽然我不确定这是否是您想要做的。正如已经说过的,你应该避免循环依赖。

Its not really clear what you are trying to do here? Activities don't communicate with each other in this way. If you want one activity to start another you need to do so using Intents:

For example if you want to the Model activity from Loggs you would issue the following commands:

Intent i = new Intent(this, Model.class);
this.startActivity(i);

Although I'm not sure if this is what you are trying to do. As has been already said you should avoid circular dependencies.

居里长安 2024-12-13 07:10:45

看一下代码。执行“log = new Loggs();”不会调用 Loggs 上的“onCreate”方法。这意味着“setContentView”永远不会被调用。

在 Loggs#click 方法中,通过“findViewById”获得的按钮将为 null。结果 btn.setEnabled 会导致 NullPointerException 导致程序崩溃。

WarrenFaith 和 blackbelt 给出了很好的建议。了解活动、何时、如何以及为何应使用它们。

Look at the code. Doing "log = new Loggs();" does not call the "onCreate" method on Loggs. Which means that "setContentView" is never called.

At the Loggs#click method, the button that you get via "findViewById" will be null. As a result btn.setEnabled would cause a NullPointerException causing the program to crash.

WarrenFaith and blackbelt gives good advice. Read up on activities, when, how and why they should be used.

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