在 Android 中管理两个 Activity 时出现问题
我想做的事情非常简单。我创建了两个类 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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
它不太清楚你想在这里做什么?活动不会以这种方式相互通信。如果您希望一个活动启动另一个活动,您需要使用意图来执行此操作:
例如,如果您想从 Loggs 进行模型活动,您将发出以下命令:
虽然我不确定这是否是您想要做的。正如已经说过的,你应该避免循环依赖。
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:
Although I'm not sure if this is what you are trying to do. As has been already said you should avoid circular dependencies.
看一下代码。执行“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.