Aufruf von mehreren Activities bringt die app zum Absturz

darodi2012

Mitglied
Hallo zusammen,

zu meinem Problem. Ich verzweifle komplett und komme nicht weiter.
Ich Programmiere ein Buch als App mit integrierten Entscheidungen.
Nun wenn ich die erste Activity aufrufe und dann die nächste, dann ist alles i.O. wenn ich dann bei der Seite 2 bin und auf Seite 3 springen möchte, dann kommt der Absturz. Bluescreen war auch 2 mal da. die Seiten1 bis 3 sind sehr identisch aufgebaut. java-Code der ersten 4 Activities siehe unten.
Vor der 3ten Seite (4.Activity ) stürzt das Programm ab. von Seite 2(3.Activity) springt er komischerweise auf Seite 1(2.Activity). Wenn ich das nochmal weiterklicke dann kommt der Absturz.

MainActivity:
Java:
package davrstudio.soulofdeath;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
private Button btn1,btn2,btn3,btn4;

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

btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
nextActivity1();
}
});

btn4 = (Button) findViewById(R.id.btn4);
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
finish();
System.exit(0);
}
});
}
public void nextActivity1(){
Intent intent = new Intent(this, Seite1.class);
startActivity(intent);
}

}

Seite1:
Java:
package davrstudio.soulofdeath;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Seite1 extends AppCompatActivity {
private Button btn1,btn2,btn3;

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

btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
nextActivity1();
}
});
}
public void nextActivity1(){
Intent intent = new Intent(this, Seite2.class);
startActivity(intent);
}
}

Seite2:
Java:
package davrstudio.soulofdeath;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Seite2 extends AppCompatActivity {
private Button btn1,btn2,btn3;

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

btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
nextActivity1();
}
});
}
public void nextActivity1(){
Intent intent = new Intent(this, Seite3.class);
startActivity(intent);
}
}
Seite3:
Java:
package davrstudio.soulofdeath;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Seite3 extends AppCompatActivity {
    private Button btn1, btn2, btn3;
    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seite3);
        btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                nextActivity1();
            }
        });
        btn2 = (Button) findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                nextActivity2();
            }
        });
    }
    public void nextActivity1() {
        Intent intent = new Intent(this, Seite4.class);
        startActivity(intent);
    }
    public void nextActivity2() {
        Intent intent = new Intent(this, Seite5.class);
        startActivity(intent);
    }
}
 
Zuletzt bearbeitet von einem Moderator:
Was bekommst Du denn für eine Exception? Den Code bitte künftig in Code-Tags posten (die drei Punkte neben Smiley, dort Code, dann Java, in das Textfeld einfügen). Wenn Du den Beitrag noch bearbeiten kannst, kannst Du das noch machen, sonst kümmert sich vielleicht ein Moderator darum.
 
Ok.

das bekomme ich raus:
Java:
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: davrstudio.soulofdeath, PID: 1966
    java.lang.RuntimeException: Unable to start activity ComponentInfo{davrstudio.soulofdeath/davrstudio.soulofdeath.Seite3}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3341)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3485)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2045)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7478)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at davrstudio.soulofdeath.Seite3.onCreate(Seite3.java:25)
        at android.app.Activity.performCreate(Activity.java:7989)
        at android.app.Activity.performCreate(Activity.java:7978)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3316)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3485) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2045) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7478) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
 
Zuletzt bearbeitet von einem Moderator:
Im onCreate bekommst du eine NPE weil er wohl einen der beiden Buttons nicht finden kann. Dadurch ist die Variable null und der Aufruf von setOnClickListener wirft die NPE.

Edit: kannst bei dir in Zeile 25 nachsehen, welche Zeile das ist ....
 
Vielen Dank für die schnelle Antworten.

Der Fehler war in der XML-Datei.
Die id von btn2 Button war falsch in der XML. Bzw hat nicht zu Java gepasst.

Hab ewig in der Java-Datei gesucht. XML hab ich mir zwar auch angeschaut aber die Buttons scheinbar nicht. Hab versehentlich den falschen Button gelöscht.

Top danke!
 

Zurück
Oben