为什么不能从其他类(其他java文件)访问按钮值?
我正在尝试构建一个android应用程序,它可以让用户在限定的时间内进行一些计算。代码运行良好,直到我将代码分为两部分并创建了另一个类来执行其他任务 我已经将所有相应的包和类文件导入到新类中。代码中没有错误,但当我运行应用程序时,按钮和文本视图没有显示任何内容。我尝试更改代码多次,但没有任何用处。当我将所有代码合并到一个类中时,代码运行良好 错误内容如下: 尝试调用虚拟方法 Logic.java中的“b1.setText(Integer.toString(answers[0]))” 每个按钮和文本视图都显示相同的错误。此错误是为什么不能从其他类(其他java文件)访问按钮值?,java,android,Java,Android,我正在尝试构建一个android应用程序,它可以让用户在限定的时间内进行一些计算。代码运行良好,直到我将代码分为两部分并创建了另一个类来执行其他任务 我已经将所有相应的包和类文件导入到新类中。代码中没有错误,但当我运行应用程序时,按钮和文本视图没有显示任何内容。我尝试更改代码多次,但没有任何用处。当我将所有代码合并到一个类中时,代码运行良好 错误内容如下: 尝试调用虚拟方法 Logic.java中的“b1.setText(Integer.toString(answers[0]))” 每个按钮和文
空指针异常
。我如何让它工作?感谢您的帮助
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!");
}
};
}
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();
}
}
}
Logic.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!");
}
};
}
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();
}
}
}
这里的主要问题是,为什么当代码仅在主类中时应用程序工作正常,而当某些代码在其他类中编写时应用程序不工作?
谢谢。这是因为你应该传递上下文,并做一些技巧来做到这一点 我看到两种解决方案: (一) 执行所需操作的最简单方法是,使用:
public-String-operatio()
并返回要设置的字符串,而不是使用public-void-operatio()
然后大体上,这样称呼它:
textView.setText(c.operatio())
将设置返回的字符串
2) 使用上下文
将MainActivity的上下文传递到函数中
逻辑类:
该功能应为:
public void operatio(Context context)
在逻辑类中,使用:
TextView txtView = (TextView) ((Activity)context).findViewById(R.id.sum);
textview.setText(a1 + "+" + a2);
主要活动
然后,调用它:c.operatio(MainActivity.this)代码>