java在安卓的主活动类中调用另一个类
我对应用程序编程没有真正的经验,所以解决方案可能很简单
我正在制作一个应用程序来显示纸牌游戏的分数,我想在另一个类中存储某些值,即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];
}
}
然而,当我试图创建圆形对象时,我的应用程序崩溃了。 这是关于主类中的代码部分
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 楼答案
问题是您初始化了
round
变量数组,但没有初始化round
项以下是正确的函数:
我还建议修改
initialize
函数,使其成为构造函数