我正在尝试构建一个Android应用程序,使用户可以在一个时间限制内进行一些计算。代码运行良好,直到我将代码分成两部分并创建另一个类来执行其他任务。
我已经将所有相应的包和类文件导入到新类中。代码中没有错误但是当我运行应用程序时,按钮和textview没有显示任何内容。我尝试多次更改代码但没有用。当我将所有代码组合到一个类中时,代码运行良好。
错误如下:
尝试在Logic.java中调用虚方法“b1.setText(Integer.toString(answers [0]))”。
每个按钮和textview显示相同的错误。这个错误是Nullpointer Exception
。我如何使它工作?任何帮助表示赞赏。
main activity.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 void operatio()
,使用:public String operatio()
并返回你想要设置的字符串。
然后在main中,将其称为:
textView.setText( c.operatio());
将设置您返回的String。
2)使用上下文。
您将MainActivity的上下文传递给函数。
逻辑类:
功能应该是:
public void operatio(Context context)
在您的逻辑类中,使用:
TextView txtView = (TextView) ((Activity)context).findViewById(R.id.sum);
textview.setText(a1 + "+" + a2);
主要活动
然后,称之为:c.operatio(MainActivity.this);