gpt4 book ai didi

java - 为什么无法从另一个类(其他 java 文件)访问按钮值?

转载 作者:行者123 更新时间:2023-12-02 10:00:24 25 4
gpt4 key购买 nike

我正在尝试构建一个Android应用程序,让用户在有限的时间内进行一些计算。该代码运行良好,直到我将代码分为两部分并创建另一个类来执行其他任务。

我已将所有相应的包和类文件导入到新类中。代码中没有错误,但当我运行应用程序时,按钮和 TextView 不显示任何内容。我尝试多次更改代码,但没有使用。当我将所有代码合并到一个类中时,代码运行良好。

错误内容为:

attempt to invoke a virtual method "b1.setText(Integer.toString(answers[0]))" in Logic.java.

每个按钮和 TextView 都显示相同的错误。此错误是一个空指针异常。我该如何让它发挥作用?如有任何帮助,我们将不胜感激。

MainActivity.java

package e.nani.firstattempt;

import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
public int a1;//random num 1
public int a2;//random num 2;
public TextView textview;
public Button b1;
public Button b2;
public Button b3;
public Button b4;
public int option1;
public int option2;
public int option3;
public int option4;
public int score = 0;
TextView scoreid;
int numberofquestions = 10;
TextView time;
public int answers[] = new int[4];
Logic c;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView) findViewById(R.id.sum);
b1 = (Button) findViewById(R.id.option1);
b2 = (Button) findViewById(R.id.option2);
b3 = (Button) findViewById(R.id.option3);
b4 = (Button) findViewById(R.id.option4);
time = (TextView) findViewById(R.id.timer);

scoreid = (TextView) findViewById(R.id.scoreid);
scoreid.setText((0 + "/" + numberofquestions));


c.operatio();

timer.start();

}


public void operation(View V) {
try {
switch (V.getId()) {
case R.id.option1:

if (b1.getText().equals(Integer.toString(option4))) {
score = score + 1;
c.operatio();
scoreid.setText((score + "/" + numberofquestions));
} else {

Toast.makeText(this, "wrong answer", Toast.LENGTH_SHORT).show();
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
c.operatio();
}
break;
case R.id.option2:


if (b2.getText().equals(Integer.toString(option4))) {
score = score + 1;
c.operatio();
scoreid.setText(score + "/" + numberofquestions);
} else {

Toast.makeText(this, "wrong answer", Toast.LENGTH_SHORT).show();
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
c.operatio();
}
break;
case R.id.option3:
if (b3.getText().equals(Integer.toString(option4))) {
score = score + 1;
c.operatio();
scoreid.setText((score + "/" + numberofquestions));
} else {
Toast.makeText(this, "wrong answer", Toast.LENGTH_SHORT).show();
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
c.operatio();
}
break;

case R.id.option4:
if (b4.getText().equals(Integer.toString(option4))) {
score = score + 1;
c.operatio();
scoreid.setText(score + "/" + numberofquestions);
} else {
Toast.makeText(this, "wrong answer", Toast.LENGTH_SHORT).show();
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
c.operatio();
}

break;
}


} catch (Exception e) {
e.printStackTrace();
}
}


CountDownTimer timer = new CountDownTimer(30000, 1000) {

public void onTick(long millisUntilFinished) {
time.setText("seconds remaining: " + millisUntilFinished / 1000);
}

public void onFinish() {
time.setText("done!");
}
};


}

Logic.java

package e.nani.firstattempt;

import java.util.Random;

class Logic extends MainActivity {

public void operatio() {
try {
Random n = new Random();
int n1 = n.nextInt(4);
int n2 = n.nextInt(4);
int n3 = n.nextInt(4);
int n4 = n.nextInt(4);

a1 = n.nextInt(51);
a2 = n.nextInt(35);
option1 = n.nextInt((a1 + a2) + 1);

option2 = n.nextInt((a1 + a2) + 1);
option3 = n.nextInt((a1 + a2) + 1);
option4 = a1 + a2;


answers[n1] = option1;
while (n2 == n1) {
n2 = n.nextInt(4);
}
while (option2 == option1 || option2 == option4) {

option2 = nextInt((a1 + a2) + 1);

}


answers[n2] = option2;
while (option3 == option2 || option3 == option1 || option3 == option4) {
option3 = n.nextInt((a1 + a2) + 1);
}


while (n3 == n2 || n3 == n1) {
n3 = n.nextInt(4);
}

answers[n3] = option3;

while (n4 == n2 || n4 == n1 || n4 == n3) {
n4 = n.nextInt(4);
}
answers[n4] = option4;


b1.setText(Integer.toString(answers[0]));
b2.setText(Integer.toString(answers[1]));
b3.setText(Integer.toString(answers[2]));
b4.setText(Integer.toString(answers[3]));
textview.setText(a1 + "+" + a2);
} catch (Exception e) {
e.printStackTrace();
}
}
}

这里的主要问题是,为什么当代码仅在主类中时应用程序工作正常,但当某些代码编写在其他类中时应用程序无法工作?谢谢。

最佳答案

这是因为您应该传递上下文并执行一些技巧来做到这一点。

我看到 2 个解决方案:

1)执行您想要的操作的最简单方法是使用:public String operatio(),而不是使用 public void operatio() 并返回您想要设置的字符串。

然后在 main 中这样调用:

textView.setText( c.operatio()); 这将设置您返回的字符串。

2) 使用上下文。

您将 MainActivity 的上下文传递到函数中。

LOGIC CLASS :

该函数应该是:

public void operatio(Context context)

在您的逻辑类中,使用:

TextView txtView = (TextView) ((Activity)context).findViewById(R.id.sum);
textview.setText(a1 + "+" + a2);

MAIN ACTIVITY

然后,调用它:c.operatio(MainActivity.this);

关于java - 为什么无法从另一个类(其他 java 文件)访问按钮值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55674539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com