在android

时间:2016-07-03 14:57:04

标签: java android android-studio

我在app编程方面并不是很有经验,所以解决方案可能很简单......

我正在创建一个应用程序来显示纸牌游戏的分数,我想将某些值存储在另一个类中,即Round

public class Round {
    int[] score1, score2, roem1, roem2;
    public void initialize(){
        score1 = new int[4];
        score2 = new int[4];
        roem1 = new int[4];
        roem2 = new int[4];

        for(int i = 0; i < 4; i++){
            score1[i] = 0;
            score2[i] = 0;
            roem1[i] = 0;
            roem2[i] = 0;
        }
    }
    public void setPoints(int game, int s1, int s2) {
        score1[game] = s1;
        score2[game] = s2;
    }
    public void setRoem(int game, int r1, int r2) {
        roem1[game] = r1;
        roem2[game] = r2;
    }
    public int getPoints1(int game){
        return score1[game];
    }
    public int getPoints2(int game){
        return score2[game];
    }
    public int getRoem1(int game){
        return roem1[game];
    }
    public int getRoem2(int game){
        return roem2[game];
    }
}

但是,当我尝试创建Round对象时,我的应用程序崩溃了。 它是关于主类的代码的一部分。

public class MainActivity extends AppCompatActivity {
    Round[] round;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Thread(new Runnable() {
            @Override
            public void run() {
                round = new Round[4];
                for(int i = 0; i < 4; i++){
                    round[i].initialize();
                }
            }
        }).start();
    }
}

我认为在活动中使用另一个类会使应用程序崩溃,但我不知道如何解决它。 这是完整的主要活动类

public class MainActivity extends AppCompatActivity {

    TextView roundNr, gameNr;
    Button roundPlus, roundMinus;
    Button gamePlus, gameMinus;
    EditText points1, points2;

    Round[] round;

    String team1, team2;

    int roundNumber = 1, gameNumber = 1;
    int score1Total, score2Total, roem1Total, roem2Total;
    int p;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Thread(new Runnable() {
            @Override
            public void run() {
                round = new Round[4];
                for(int i = 0; i < 4; i++){
                    round[i].initialize();
                }
            }
        }).start();

        roundNr = (TextView) findViewById(R.id.roundNr);
        roundNr.setText("1");

        gameNr = (TextView) findViewById(R.id.gameNr);
        gameNr.setText("1");

        roundPlus = (Button) findViewById(R.id.roundPlus);
        roundPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundNumber++;
                if (roundNumber <= 4) {
                    roundNr.setText(Integer.toString(roundNumber));
                }
                else {
                    roundNumber--;
                }
            }
        });
        roundMinus = (Button) findViewById(R.id.roundMinus);
        roundMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundNumber--;
                if (roundNumber >= 1) {
                    roundNr.setText(Integer.toString(roundNumber));
                }
                else {
                    roundNumber++;
                }
            }
        });

        gamePlus = (Button) findViewById(R.id.gamePlus);
        gamePlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gameNumber++;
                if (gameNumber <= 4) {
                    gameNr.setText(Integer.toString(gameNumber));
                }
                else {
                    roundNumber++;
                    if (roundNumber <= 4) {
                        roundNr.setText(Integer.toString(roundNumber));
                        gameNumber = 1;
                        gameNr.setText(Integer.toString(gameNumber));
                    }
                    else {
                        roundNumber--;
                        gameNumber--;
                    }
                }
            }
        });
        gameMinus = (Button) findViewById(R.id.gameMinus);
        gameMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gameNumber--;
                if (gameNumber >= 1) {
                    gameNr.setText(Integer.toString(gameNumber));
                }
                else {
                    roundNumber--;
                    if (roundNumber >= 1) {
                        roundNr.setText(Integer.toString(roundNumber));
                        gameNumber = 4;
                        gameNr.setText(Integer.toString(gameNumber));
                    }
                    else {
                        roundNumber++;
                        gameNumber++;
                    }
                }
            }
        });

        points1 = (EditText) findViewById(R.id.points1);
        points1.setText("0");
        points1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!s.toString().equals("")){
                    p = Integer.parseInt(s.toString());
                    points2.setText(Integer.toString(162 - p));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        points2 = (EditText) findViewById(R.id.points2);
        points2.setText("0");
    }
}

1 个答案:

答案 0 :(得分:2)

问题是您初始化round变量数组,而不是round项。

这里有正确的功能:

public class MainActivity extends AppCompatActivity {
    Round[] round;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Thread(new Runnable() {
            @Override
            public void run() {
                round = new Round[4];
                for(int i = 0; i < 4; i++){
                    round[i] = new Round();
                    round[i].initialize();
                }
            }
        }).start();
    }
}

我还建议修改initialize函数并使其成为构造函数。