请你帮我解决错误" java.lang.NullPointerException"。 当我尝试通过" Neues Spiel"来启动我的QuizActivity.java时,我的应用程序在模拟器中崩溃了。 MainActivity.java中的按钮。 (我清理了项目并尝试了不同的模拟器) 我寻找了一些答案并尝试了不同的东西,但我没有发现我的错误,也许是因为我是一个android-java新手。
MainActivity.java
package com.example.app_name;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity{
TextView titel;
Typeface font_alexbrush;
long score=0;
@Override
public void onBackPressed() {}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Context context = this;
final Button Beenden_btn = (Button) findViewById(R.id.Beenden_btn);
final Button NeuesSpiel = (Button) findViewById(R.id.NeuesSpiel_btn);
final Button Impressum = (Button) findViewById(R.id.Impressum_btn);
Typeface font_alexbrush = Typeface.createFromAsset(getAssets(), "Font/alexbrush.ttf");
TextView titel= (TextView) findViewById(R.id.titel);
titel.setTypeface(font_alexbrush);
Impressum.setOnClickListener(new OnClickListener(){
public void onClick(View x) {
Intent intent = new Intent(getApplicationContext(), Impressum.class);
startActivity(intent);}});
NeuesSpiel.setOnClickListener(new OnClickListener(){
public void onClick(View x) {
Intent intent = new Intent(getApplicationContext(), QuizActivity.class);
startActivity(intent);
finish();}});
Beenden_btn.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder .setMessage("Soll HistoryQuiz beendet werden?")
.setCancelable(false)
.setPositiveButton("Ja",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) { finish();
System.exit(0);}})
.setNegativeButton("Nein",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {dialog.cancel();}});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();}});}}
logcat的
05-17 13:50:22.241: E/AndroidRuntime(3636): Caused by: java.lang.NullPointerException
05-17 13:50:22.241: E/AndroidRuntime(3636): at com.example.app_name.QuizActivity.onCreate(QuizActivity.java:39)
05-17 13:50:22.241: E/AndroidRuntime(3636): at android.app.Activity.performCreate(Activity.java:5133)
05-17 13:50:22.241: E/AndroidRuntime(3636): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-17 13:50:22.241: E/AndroidRuntime(3636): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-17 13:50:22.241: E/AndroidRuntime(3636): ... 11 more
05-17 13:58:15.853: E/OpenGLRenderer(3679): Getting MAX_TEXTURE_SIZE from GradienCache
05-17 13:58:15.857: E/OpenGLRenderer(3679): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
05-17 13:58:16.617: E/AndroidRuntime(3679): FATAL EXCEPTION: main
05-17 13:58:16.617: E/AndroidRuntime(3679): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app_name/com.example.app_name.QuizActivity}: java.lang.NullPointerException
05-17 13:58:16.617: E/AndroidRuntime(3679): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-17 13:58:16.617: E/AndroidRuntime(3679): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-17 13:58:16.617: E/AndroidRuntime(3679): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-17 13:58:16.617: E/AndroidRuntime(3679): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-17 13:58:16.617: E/AndroidRuntime(3679): at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 13:58:16.617: E/AndroidRuntime(3679): at android.os.Looper.loop(Looper.java:137)
05-17 13:58:16.617: E/AndroidRuntime(3679): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-17 13:58:16.617: E/AndroidRuntime(3679): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 13:58:16.617: E/AndroidRuntime(3679): at java.lang.reflect.Method.invoke(Method.java:525)
05-17 13:58:16.617: E/AndroidRuntime(3679): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-17 13:58:16.617: E/AndroidRuntime(3679): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-17 13:58:16.617: E/AndroidRuntime(3679): at dalvik.system.NativeStart.main(Native Method)
05-17 13:58:16.617: E/AndroidRuntime(3679): Caused by: java.lang.NullPointerException
05-17 13:58:16.617: E/AndroidRuntime(3679): at com.example.app_name.QuizActivity.onCreate(QuizActivity.java:39)
05-17 13:58:16.617: E/AndroidRuntime(3679): at android.app.Activity.performCreate(Activity.java:5133)
05-17 13:58:16.617: E/AndroidRuntime(3679): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-17 13:58:16.617: E/AndroidRuntime(3679): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-17 13:58:16.617: E/AndroidRuntime(3679): ... 11 more
答案 0 :(得分:0)
您在问题frage.setText(currentQ.getQUESTION());
上获得了一个NPE,因为currentQ
在您的问题中为空。
在使用之前,您必须初始化currentQ
。因此,要解决错误添加,
currentQ= new Question();
使用之前。也就是说,在行
之后添加上面的行quesList=db.getAllQuestions();
因此,它应该像
quesList=db.getAllQuestions();
currentQ= new Question();
答案 1 :(得分:0)
显然在这一行中失败了:
frage.setText(currentQ.getQUESTION());
这将得出frage
或currentQ
为空引用的结论。基于此,您将在此行中获得frage
的实例:
frage = (TextView)findViewById(R.id.frage);
我将currentQ
作为空引用。
因为您要根据条件
实例化此变量if(quesList!= null && quesList.size() !=0){
currentQ=quesList.get(qid);}
}
所以questList
为空或空。你是对的,这是一个经典的noob开发者错误(不仅仅是Android)。你必须确保currentQ变量在使用它之前有一个引用,或者要求一个有效的引用,你会没事的
if (currentQ != null) {
frage.setText(currentQ.getQUESTION());
a.setText(currentQ.getOPTA());
b.setText(currentQ.getOPTB());
c.setText(currentQ.getOPTC());
d.setText(currentQ.getOPTD());
}
答案 2 :(得分:0)
错误(NullPointerException)表示您正在尝试访问QuizActivity第39行的null变量。如果算得上,那就是:
frage.setText(currentQ.getQUESTION());
这意味着当时frage或currentQ为null。我的猜测是currentQ,但为了确保,你可以像这样记录:
Log.d("Tag", "Is frage null? " + (frage == null));
Log.d("Tag", "Is currentQ null? " + (currentQ == null));
您将在logcat中看到结果(我假设您知道如何使用它,因为它不在主题上)。
如果发现frage为null,则表示findViewById(R.id.frage)
返回null。如果活动无法在setContentView()
的布局集中找到具有给定ID的视图,则会出现这种情况。因此,视图的ID错误或布局错误。
如果您发现currentQ
为空,则表示您在尝试对象之前尚未启动对象。在这种情况下,您可以在以下条件下实例化currentQ
:
quesList!= null && quesList.size() !=0
这意味着情况并非如此。
希望这有帮助