Please could you help me with the error "java.lang.NullPointerException". My application crashes in the emulator when I try to start my QuizActivity.java through the "Neues Spiel" Button from the MainActivity.java. (I cleaned the project and tried a different emulators) I looked for some answers and tried different things but i didn't find my mistake, maybe it is because I am an android-java newbie.
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