unfortunately {app_name} has stopt

  • 2 Antworten
  • Letztes Antwortdatum
N

noxTo

Neues Mitglied
0
hi,
also ich bin absoluter anfänger und neu hier im forum.

mein problem:

ich arbeite gerade ein tutorial durch und es hackt schon an der ersten app.

die app bricht mit der im titel angegebenen fehlermeldung ab sobald man auf start klickt. der schmeisst ne nullpoint exception aber ich kriege es nicht hin heraus zu finden warum.

der code der main_activity
Code:
package de.bwe.kissthefrog;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class MainActivity extends Activity implements View.OnClickListener {

    private int points;
    private int round;
    private int countdown;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showStartFragment();
    }

    private void newGame(){
        points=0;
        round=1;
        initRound();

    }

    private void initRound() {
        countdown=10;
        update();
    }

    private void fillTextView(int id, String text){
        TextView tv = (TextView)findViewById(id);
        tv.setText(text);
    }

    private void update(){
        fillTextView(R.id.points, Integer.toString(points));
        fillTextView(R.id.round, Integer.toString(round));
        fillTextView(R.id.countdown, Integer.toString(countdown));
    }

    private void showStartFragment(){
        ViewGroup container = (ViewGroup)findViewById(R.id.container);
        container.removeAllViews();
        container.addView(getLayoutInflater().inflate(R.layout.fragment_start, null));
        container.findViewById(R.id.start).setOnClickListener(this);
    }

    private void showGameOverFragment(){
        ViewGroup container = (ViewGroup)findViewById(R.id.container);
        container.addView(getLayoutInflater().inflate(R.layout.fragment_gameover, null));
        container.findViewById(R.id.game_over).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.start){
            startGame();
        } else if (view.getId() == R.id.play_again){
            showStartFragment();
        }
    }

    private void startGame() {
        newGame();
    }
}
und logcat

Code:
02-24 13:15:53.334    3087-3087/de.bwe.kissthefrog E/AndroidRuntime FATAL EXCEPTION: main
    Process: de.bwe.kissthefrog, PID: 3087
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
            at de.bwe.kissthefrog.MainActivity.fillTextView(MainActivity.java:37)
            at de.bwe.kissthefrog.MainActivity.update(MainActivity.java:41)
            at de.bwe.kissthefrog.MainActivity.initRound(MainActivity.java:32)
            at de.bwe.kissthefrog.MainActivity.newGame(MainActivity.java:26)
            at de.bwe.kissthefrog.MainActivity.startGame(MainActivity.java:69)
            at de.bwe.kissthefrog.MainActivity.onClick(MainActivity.java:62)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 
Hallo,
der Fehler kommt durch einen Nullpointer in Zeile 37 zu Stande.
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at de.bwe.kissthefrog.MainActivity.fillTextView(MainActivity.java:37)


Mein Tipp: Lade (bzw. initialisiere) erst mal deine UI ordentlich, dann werden die Daten an die TextViews übertragen. Sprich in OnCreate bzw. OnCreateView lädst du deine GUI-Elemente und greifst dann auf diese zurück. Das ist meiner Meinung nach der bessere Ansatz für Leute, die sich neu in Android einarbeiten.

Beste Grüße
 
Hallo noxTo, ich bin neugierig. Ich würde mich freuen, wenn Du mir ein Link vom Tutorial schicken würdest.
 

Ähnliche Themen

G
Antworten
0
Aufrufe
132
Gerdchen07
G
G
Antworten
1
Aufrufe
384
Gerdchen07
G
G
Antworten
13
Aufrufe
599
Gerdchen07
G
L
Antworten
2
Aufrufe
555
Lexub
L
migi01
Antworten
26
Aufrufe
1.990
migi01
migi01
Zurück
Oben Unten