Android - java.lang.NullPointerException:尝试调用虚方法' void android.widget.TextView.setText ...'在null对象引用上

时间:2016-04-12 14:04:54

标签: java android

我知道之前已经多次询问过,但即使查看类似的帖子,我也无法弄清楚这一点。

这是我的MainActivity类:

package binchtitsinc.adventuregameamlior;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private int compteur;
    private TextView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        compteur = 0;
        display = (TextView) findViewById(R.id.Afficheur);
        display.setText("Count is: " + compteur);


        Game theGame;

        theGame = new Game();

        theGame.play();

    }


    public void plusOne(View button){
        compteur=compteur+1;
        display.setText("Count is: "+compteur);

    }

    public void toUser(String texte){
        display.setText("Message:"+texte);
    }
}

我已经检查过,ID是正确的(R.id.Afficheur)。 真正奇怪的是,当我在该课堂上打电话给用户时,它的工作完美无瑕。但是,当我从另一个类调用该方法时(这里," Game"),我得到了我在标题中粘贴的错误。

这是我课堂游戏的开始:

package binchtitsinc.adventuregameamlior;

/**
 * Created by Leo on 11/04/16.
 */
public class Game {

    //Attributes

    private Parser parser;
    private Energy energy;
    private Room currentRoom;
    private Room goalRoom;
    private MainActivity main;
    private DirectionLexicon dirVocabulary;
    private CommandLexicon commandVocabulary;
    private boolean wantsToQuit;
    private boolean hasTheItem;

    //Methods

    //Public methods

    /**
     * This is a constructor.
     * It creates the game and initialise its internal map.
     */
    public Game() {
        dirVocabulary = new DirectionLexicon();
        commandVocabulary = new CommandLexicon();
        createRooms();


        energy = new Energy();
        main = new MainActivity();

        parser = new Parser(2);  //the parameter 2 here in the call to the constructor of Parser means that in this game
        // sentences typed in by the player will contain at most 2 words.
    }


    /**
     * Main play routine.  Loops until end of play.
     */
    public void play() {
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.

        wantsToQuit = false;
        hasTheItem = false;
        while (!wantsToQuit) {
            if (!currentRoom.isSameRoomAs(goalRoom) || !hasTheItem) {
                // && is the logical operator AND
                // ! is the logical operator NOT
                // currentRoom.isSameRoomAs(goalRoom) checks whether we have reached the goal.
                parser.getAndAnalyzeSentence();
                if (parser.lastSentenceLength() > 0) {// Here we know the player has typed in at list 1 word
                    if (commandVocabulary.isLexiconWord(parser.word(1))) { // here we know the firs word typed in corresponds to a known command
                        if (energy.getEnergy() <= energy.move) { // If the remaining energy is less than the amount required to move, it's over
                            main.toUser("You don't have any more energy. You're dead.");

printwelcome()调用main.toUser()方法,它也在粘贴代码的末尾:main.toUser(&#34;你没有更多的精力。你和#39;死了&#34;)。

有什么想法吗?

0 个答案:

没有答案