[英]Intent in condition doesn't work
I have tried searching for this topic but i didn't find anything that would help me, so here I am asking this.我试过搜索这个话题,但我没有找到任何对我有帮助的东西,所以我在这里问这个。
I am a beginner so I don't understand a lot of terms and if you answer my question please try to use simple language so I could understand.我是初学者,所以我不懂很多术语,如果您回答我的问题,请尝试使用简单的语言,以便我理解。
I have a condition in that the elements at same position of two lists are compared and if they aren't equal than it jumps to another activity:我有一个条件,即比较两个列表相同位置的元素,如果它们不相等,则跳转到另一个活动:
if (randomColors.get(i) != userColors.get(i)) {
Intent i = new Intent( GameActivity.this, GameOverActivity.class);
startActivity(i);
}
and it displays an error in debugging that I cannot solve:它在调试中显示一个我无法解决的错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gabie212.simonsays/com.gabie212.simonsays.GameOverActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.gabie212.simonsays/com.gabie212.simonsays.GameOverActivity}: java.lang.NullPointerException: 尝试调用虚拟方法 'void android.view.View.setOnClickListener(android. view.View$OnClickListener)' 在一个空对象引用上
Please help me, I am a beginner and I don't know what's wrong with my code because I have done exactly as they taught us in class...请帮助我,我是初学者,我不知道我的代码有什么问题,因为我完全按照他们在课堂上教我们的方式完成了...
Here is the complete code I know its not the best but its a work in proggress这是完整的代码,我知道它不是最好的,但它正在进行中
package com.gabie212.simonsays;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;
public class GameActivity extends AppCompatActivity implements
View.OnClickListener {
private int i = 0;
private Thread t = new Thread();
private Button greenButton;
private Button redButton;
private Button blueButton;
private Button yellowButton;
private Button startButton;
private ArrayList<Integer> randomColors = new ArrayList<Integer>();
private ArrayList<Integer> userColors = new ArrayList<Integer>();
private GameManger gm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gm = new GameManger(this);
setContentView(R.layout.activity_game);
greenButton = (Button) findViewById(R.id.btnGreen);
redButton = (Button) findViewById(R.id.btnRed);
blueButton = (Button) findViewById(R.id.btnBlue);
yellowButton = (Button) findViewById(R.id.btnYellow);
startButton = (Button) findViewById(R.id.btnStart);
startButton.setOnClickListener(this);
greenButton.setOnClickListener(this);
redButton.setOnClickListener(this);
blueButton.setOnClickListener(this);
yellowButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int num;
num= gm.getColor(4);
randomColors.add(num);
android.os.Handler handler = new android.os.Handler();
//TODO if the start button is pressed multiple times simultaneously it starts the lightup loop multiple times simultaneously
if (v.getId() == startButton.getId()) {
for (i = 0; i < randomColors.size(); i++) //light up loop
{
switch (randomColors.get(i)) {
case 1:
greenButton.setBackgroundResource(R.drawable.greenlightup);
handler.postDelayed(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
greenButton.setBackgroundResource(R.drawable.green);
}
}, 2000);
break;
case 2:
redButton.setBackgroundResource(R.drawable.redlightup);
handler.postDelayed(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
redButton.setBackgroundResource(R.drawable.red);
}
}, 2000);
break;
case 3:
blueButton.setBackgroundResource(R.drawable.bluelightup);
handler.postDelayed(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
blueButton.setBackgroundResource(R.drawable.blue);
}
}, 2000);
break;
case 4:
yellowButton.setBackgroundResource(R.drawable.yellowlightup);
handler.postDelayed(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
yellowButton.setBackgroundResource(R.drawable.yellow);
}
}, 2000);
break;
}
handler.postDelayed(new Runnable()
{
public void run() {
}
}, 2000);
}
for(i=0;i<randomColors.size();i++)
{
if(v.getId()==greenButton.getId())
{
userColors.add(1);
}
else
{
if(v.getId()==redButton.getId()){
userColors.add(2);
}
else
{
if(v.getId()==blueButton.getId())
{
userColors.add(3);
}
else
{
userColors.add(4);
}
}
}
}
for(i=0;i<randomColors.size();i++)
{
if(randomColors.get(i)!=userColors.get(i))
{
Intent i = new Intent( GameActivity.this, GameOverActivity.class);
startActivity(i);
}
}
}
}
}
by the its not a simple null pointer exception, at least i don't think so because there is nothing here to be null, there is only a simple intent in an if statement由于它不是一个简单的空指针异常,至少我不这么认为,因为这里没有什么可以为空的,if 语句中只有一个简单的意图
here's the code for the game over activity这是游戏结束活动的代码
package com.gabie212.simonsays;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
public class GameOverActivity extends AppCompatActivity implements View.OnClickListener {
private Button againButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_over);
againButton = (Button) findViewById(R.id.btnStart);
againButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent in = new Intent(GameOverActivity.this, GameActivity.class);
startActivity(in);
}
}
Thanks in advance.提前致谢。
In your GameOverActivity
, on this line:在您的
GameOverActivity
,在这一行:
againButton = (Button) findViewById(R.id.btnStart);
Your againButton
is null
as you are referencing btnStart
from your main activity layout ( activity_game.xml
).当您从主活动布局 (
activity_game.xml
) 引用btnStart
,您的againButton
为null
。 This can't be found because your GameOverActivity
is using activity_game_over.xml
for its layout:这无法找到,因为您的
GameOverActivity
使用activity_game_over.xml
作为其布局:
setContentView(R.layout.activity_game_over);
Any views need to be defined within the activity_game_over.xml
file for them to be used by the activity.任何视图都需要在
activity_game_over.xml
文件中定义,以便activity_game_over.xml
使用它们。 You need to reference the ID of the button as defined in layout/activity_game_over.xml
您需要引用
layout/activity_game_over.xml
定义的按钮 ID
againButton = (Button) findViewById(R.id.btnAgain); // Or whatver you've named it
As for your specific question as to why the Intent
is not working, the Intent
itself is working fine, it's just that GameOverActivity
cannot start up properly due to the above issue, and therefore your app crashes.至于您关于为什么
Intent
不工作的具体问题, Intent
本身工作正常,只是由于上述问题GameOverActivity
无法正常启动,因此您的应用程序崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.