简体   繁体   中英

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

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

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:

againButton = (Button) findViewById(R.id.btnStart);

Your againButton is null as you are referencing btnStart from your main activity layout ( activity_game.xml ). This can't be found because your GameOverActivity is using activity_game_over.xml for its layout:

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. You need to reference the ID of the button as defined in layout/activity_game_over.xml

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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