简体   繁体   English

在Android中管理两个活动时遇到问题

[英]Problem managing two activities in Android

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 AI am create a button. 我创建了两个类A和B。我在A中创建了一个单击处理程序,该单击处理程序在B中调用了一个函数,然后又在A中调用了一个函数。在AI中的被调用函数中,创建了一个按钮。 My programs is being forced close when I try to push the button. 当我尝试按下按钮时,我的程序被强制关闭。

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

is R.id.startButton in R.layout.main? R.layout.main中的R.id.startButton是什么? Than.. you cannot instantiate an activity with new, imo, because the default constructor for activity are private (i think). 比..您不能用新的imo实例化活动,因为活动的默认构造函数是私有的(我认为)。 Take a look at intent 看一下意图

Look at the code. 看代码。 Doing "log = new Loggs();" 进行“ log = new Loggs();” does not call the "onCreate" method on Loggs. 不会在Loggs上调用“ onCreate”方法。 Which means that "setContentView" is never called. 这意味着永远不会调用“ setContentView”。

At the Loggs#click method, the button that you get via "findViewById" will be null. 在Loggs#click方法中,通过“ findViewById”获得的按钮将为空。 As a result btn.setEnabled would cause a NullPointerException causing the program to crash. 结果,btn.setEnabled会导致NullPointerException导致程序崩溃。

WarrenFaith and blackbelt gives good advice. WarrenFaith和blackbelt提供了很好的建议。 Read up on activities, when, how and why they should be used. 阅读有关活动,何时,如何以及为何使用活动的信息。

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: 如果您希望一个活动开始另一个活动,则需要使用Intents来进行:

For example if you want to the Model activity from Loggs you would issue the following commands: 例如,如果要从Loggs进行“模型”活动,则可以发出以下命令:

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. 如前所述,您应该避免循环依赖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM