每当我在模拟器中打开活动Testme
时,都会遇到RuntimeException
。 logcat发布在下面:
FATAL EXCEPTION: main
Process: com.learn.earn.earnlearnapp, PID: 6622
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.learn.earn.earnlearnapp/com.learn.earn.earnlearnapp.Testme}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
代码似乎没有任何问题。我花了好几个小时试图解决这个问题。如果你能帮助我解决这个问题,那就太棒了。我的活动代码的一部分附在下面:
public class Testme extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testme);
readQuestions();
}
int qnum = 0;
private List<TopicQuestion> questionsList = new ArrayList<>();
private TextView txtquestion = findViewById(R.id.txtquestion);
private Button answer1 = findViewById(R.id.btnans1);
private Button answer2 = findViewById(R.id.btnans2);
private Button answer3 = findViewById(R.id.btnans3);
private Button answer4 = findViewById(R.id.btnans4);
private boolean correct = false;
private void readQuestions() {
InputStream is = getResources().openRawResource(R.raw.questions);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName("UTF-8"))
);
String line = "";
try {
while (
(line = reader.readLine()) != null) {
//Split the data
String[] tokens = line.split(",");
//Read the data
TopicQuestion question = new TopicQuestion();
question.setTopic(tokens[0]);
question.setQuestion(tokens[1]);
question.setAns1(tokens[2]);
question.setAns2(tokens[3]);
question.setAns3(tokens[4]);
question.setAns4(tokens[5]);
questionsList.add(question);
}
} catch (IOException e) {
Log.wtf("MyActivity","Error reading question file on line" + line, e);
e.printStackTrace();
}
{
}
txtquestion.setText(questionsList.get(qnum).getQuestion());
answer1.setText(questionsList.get(qnum).getAns1());
answer2.setText(questionsList.get(qnum).getAns3());
answer3.setText(questionsList.get(qnum).getAns2());
answer4.setText(questionsList.get(qnum).getAns4());
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
correct = (questionsList.get(qnum).getAns1()).equals(answer1.getText().toString());
}
});
answer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
correct = (questionsList.get(qnum).getAns2()).equals(answer2.getText().toString());
}
});
answer3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
correct = (questionsList.get(qnum).getAns1()).equals(answer3.getText().toString());
}
});
answer4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
correct = (questionsList.get(qnum).getAns1()).equals(answer4.getText().toString());
}
});
if (correct)
{
qnum +=1;
txtquestion.setText(questionsList.get(qnum).getQuestion());
answer1.setText(questionsList.get(qnum).getAns1());
answer2.setText(questionsList.get(qnum).getAns3());
answer3.setText(questionsList.get(qnum).getAns2());
answer4.setText(questionsList.get(qnum).getAns4());
}
else
{
txtquestion.setText(questionsList.get(qnum).getQuestion());
answer1.setText(questionsList.get(qnum).getAns1());
answer2.setText(questionsList.get(qnum).getAns3());
answer3.setText(questionsList.get(qnum).getAns2());
answer4.setText(questionsList.get(qnum).getAns4());
}
}
}
答案 0 :(得分:2)
您必须在OnCreate
之后的setContentView
中分配视图的值。
声明外面的字段:
private TextView ;
private Button; etc..
将以下行放在OnCreate
txtquestion = findViewById(R.id.txtquestion);
answer1 = findViewById(R.id.btnans1);
answer2 = findViewById(R.id.btnans2);
answer3 = findViewById(R.id.btnans3);
answer4 = findViewById(R.id.btnans4);
希望它有所帮助!