简体   繁体   English

在android主活动类中调用另一个类

[英]Calling another class in the Main activity class in android

I'm not really experienced in app programming so the solution may be simple... 我没有真正的应用程序编程经验,因此解决方案可能很简单...

I'm making an app to show scores for a cards game and I wanted to store certain values in another class, namely Round 我正在制作一个用于显示纸牌游戏得分的应用,我想将某些值存储在另一个类中,即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];
    }
}

What happens, however, when I try to create the Round object, my app crashes. 但是,当我尝试创建Round对象时,我的应用程序崩溃了。 It's about the part of the code from the main class. 这是关于主类代码的一部分。

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();
    }
}

What I think is that using another class in an activity crashes the app, but I don't know how to solve it. 我认为在活动中使用其他类会使应用程序崩溃,但我不知道如何解决它。 Here is the full main activity class 这是完整的主要活动课

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");
    }
}

The problem is that you initialize round variable array, but not round items. 问题是您初始化了round变量数组,但没有初始化round项目。

Here's the correct function: 这是正确的功能:

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();
    }
}

I also suggest to modify initialize function and make it a constructor. 我还建议修改initialize函数并将其构造为构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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