我试过搜索这个主题,但我找不到任何可以帮助我的东西,所以我在这里问这个。
我是初学者,所以我不理解很多术语,如果你回答我的问题,请尝试使用简单的语言让我明白。
我的条件是两个列表中相同位置的元素进行比较,如果它们不等于跳转到另一个活动:
if (randomColors.get(i) != userColors.get(i)) {
Intent i = new Intent( GameActivity.this, GameOverActivity.class);
startActivity(i);
}
它在调试中显示一个我无法解决的错误:
java.lang.RuntimeException:无法启动活动ComponentInfo {com.gabie212.simonsays / com.gabie212.simonsays.GameOverActivity}:java.lang.NullPointerException:尝试调用虚方法' void android.view。 View.setOnClickListener(android.view.View $ OnClickListener)'在空对象引用上
请帮助我,我是初学者,而且我不知道我的代码有什么问题,因为我完全按照他们在课堂上教给我们的那样做了...
这是完整的代码 我知道它不是最好的,但它在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);
}
}
}
}
}
它不是一个简单的空指针异常,至少我不这么认为,因为这里没有任何东西是空的,if语句中只有一个简单的意图
这里是游戏活动的代码
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);
}
}
提前致谢。
答案 0 :(得分:0)
在GameOverActivity
,在这一行:
againButton = (Button) findViewById(R.id.btnStart);
当您从主要活动布局(againButton
)引用null
时,btnStart
为activity_game.xml
。由于GameOverActivity
正在使用activity_game_over.xml
进行布局,因此找不到此内容:
setContentView(R.layout.activity_game_over);
任何视图都需要在activity_game_over.xml
文件中定义,以供活动使用。您需要引用layout/activity_game_over.xml
againButton = (Button) findViewById(R.id.btnAgain); // Or whatver you've named it
关于Intent
无法正常工作的具体问题,Intent
本身工作正常,只是GameOverActivity
无法正常启动上述问题,因此您的应用程序崩溃。