我正在做一个关于冰岛的小问答游戏。但是,当我启动第二个Activity时,应用程序崩溃并出现NullPointerException。任何人都可以找到它的位置吗?代码对我来说至少没有错......
这是崩溃的代码:
package com.example.root.myapplication;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Quiz extends AppCompatActivity {
Quest[] fragen;
int i=0;
Button btna;
Button btnb;
Button btnc;
Button btnd;
TextView solsign;
TextView frage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
fragen = new Quest[10];
mkquest();
}
public void mkquest(){
//fragen = new Quest[10];
btna = (Button) findViewById(R.id.button2);
btnb = (Button) findViewById(R.id.button3);
btnc = (Button) findViewById(R.id.button4);
btnd = (Button) findViewById(R.id.button5);
solsign = (TextView) findViewById(R.id.textView2);
frage = (TextView) findViewById(R.id.textView);
readArray();
if(fragen[0].ansA!=null) {
btna.setText(fragen[0].ansA);
btnb.setText(fragen[0].ansB);
btnc.setText(fragen[0].ansC);
btnd.setText(fragen[0].ansD);
frage.setText(fragen[0].question);
}
else {
Context context = getApplicationContext();
CharSequence text = "Exception!!!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
public void nextquest(View view){
i++;
btna.setText(fragen[i].ansA);
btnb.setText(fragen[i].ansB);
btnc.setText(fragen[i].ansC);
btnd.setText(fragen[i].ansD);
frage.setText(fragen[i].question);
solsign.setText("");
}
public void checkans(View view){
switch (view.getId()){
case R.id.button2:
if(btna.getText().toString()==fragen[i].rightans){
solsign.setText("Richtige Antwort!");
}
else solsign.setText("Falsche Antwort!");
break;
case R.id.button3:
if(btna.getText().toString()==fragen[i].rightans){
solsign.setText("Richtige Antwort!");
}
else solsign.setText("Falsche Antwort!");
break;
case R.id.button4:
if(btna.getText().toString()==fragen[i].rightans){
solsign.setText("Richtige Antwort!");
}
else solsign.setText("Falsche Antwort!");
break;
case R.id.button5:
if(btna.getText().toString()==fragen[i].rightans){
solsign.setText("Richtige Antwort!");
}
else solsign.setText("Falsche Antwort!");
break;
}
}
public class Quest{
String question="a";
String ansA="A";
String ansB="A";
String ansC="A";
String ansD="A";
String rightans="A";
}
public void readArray(){
if(fragen[0].ansA!=null) {
fragen[0].ansA = "Rejkjavik";
fragen[0].ansB = "Reykjavik";
fragen[0].ansC = "Reikjavik";
fragen[0].ansD = "Reykiavik";
fragen[0].question = "Wie heißt die Hauptstadt Islands?";
fragen[0].rightans = "Reykjavik";
fragen[1].ansA = "10.003 Mio. US$";
fragen[1].ansB = "25.003 Mio. US$";
fragen[1].ansC = "20.003 Mio. US$";
fragen[1].ansD = "18.003 Mio. US$";
fragen[1].question = "Wie hoch ist das BIP Islands (2007)?";
fragen[1].rightans = "20.003 Mio. US$";
/* fragen[0].ansA="";
fragen[0].ansB="";
fragen[0].ansC="";
fragen[0].ansD="";
fragen[0].question="";
fragen[0].rightans="";
fragen[0].ansA="";
fragen[0].ansB="";
fragen[0].ansC="";
fragen[0].ansD="";
fragen[0].question="";
fragen[0].rightans="";
fragen[0].ansA="";
fragen[0].ansB="";
fragen[0].ansC="";
fragen[0].ansD="";
fragen[0].question="";
fragen[0].rightans="";
fragen[0].ansA="";
fragen[0].ansB="";
fragen[0].ansC="";
fragen[0].ansD="";
fragen[0].question="";
fragen[0].rightans="";
fragen[0].ansA="";
fragen[0].ansB="";
fragen[0].ansC="";
fragen[0].ansD="";
fragen[0].question="";
fragen[0].rightans="";
fragen[0].ansA="";
fragen[0].ansB="";
fragen[0].ansC="";
fragen[0].ansD="";
fragen[0].question="";
fragen[0].rightans="";
fragen[0].ansA="";
fragen[0].ansB="";
fragen[0].ansC="";
fragen[0].ansD="";
fragen[0].question="";
fragen[0].rightans="";
fragen[0].ansA="";
fragen[0].ansB="";
fragen[0].ansC="";
fragen[0].ansD="";
fragen[0].question="";
fragen[0].rightans="";
*/
}
else{
Context context = getApplicationContext();
CharSequence text = "Exception!!!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
}
在这里 例外是:
06-28 09:26:38.434 11677-11677 / com.example.root.myapplication E / AndroidRuntime:FATAL EXCEPTION:主要流程: com.example.root.myapplication,PID:11677 java.lang.RuntimeException: 无法开始活动 ComponentInfo {com.example.root.myapplication / com.example.root.myapplication.Quiz}: java.lang.NullPointerException:尝试从字段读取 'java.lang.String com.example.root.myapplication.Quiz $ Quest.ansA'上的一个 null对象引用 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在android.app.ActivityThread.-wrap11(ActivityThread.java)处 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) 在android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:148)at android.app.ActivityThread.main(ActivityThread.java:5422)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
引起:java.lang.NullPointerException:尝试从字段读取 'java.lang.String com.example.root.myapplication.Quiz $ Quest.ansA'上的一个 null对象引用 com.example.root.myapplication.Quiz.readArray(Quiz.java:134)at at com.example.root.myapplication.Quiz.mkquest(Quiz.java:45)at com.example.root.myapplication.Quiz.onCreate(Quiz.java:29)at android.app.Activity.performCreate(Activity.java:6251)at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在android.app.ActivityThread.-wrap11(ActivityThread.java)处 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) 在android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:148)at android.app.ActivityThread.main(ActivityThread.java:5422)at java.lang.reflect.Method.invoke(Native Method)
在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)`
对我而言似乎所有事情都已正确初始化......
非常感谢你!
编辑: 不幸的是,像这样初始化fragen也不起作用:
for (int j=0; j<9; j++) {
fragen[j] = new Quest();
}
我想在我的.xml文件中执行onClick的类的引用。
请不要将我的问题标记为重复,因为我无法找到错误。我知道NullPointerException是什么,但对我来说代码是正确的....