Hallo Lieber Community,
ich bin neu hier und hoffe ich habe meine Frage im richtigen Bereich gestellt.
Ich bin gerade dabei eine kleine Chat App zu programmieren.
Die App funktioniert in Verbindung mit Firebase.
Reg und Login funktionieren einwandfrei, auch meine Untermenüs machen bisher keine Probleme.
Allerdings habe ich ein Problem mit meiner SettingsActivity.
Bein Versuch in diese zu wechseln crasht meine App und ich erhalte folgenden Fehler :
AndroidRuntime: FATAL EXCEPTION: main
Process: com.dojoapps.mfu, PID: 10066
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dojoapps.mfu/com.dojoapps.mfu.SettingsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at com.dojoapps.mfu.SettingsActivity.onCreate(SettingsActivity.java:51)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 10066 SIG: 9
Application terminated.
Display More
Hier meine SettingsActivity:
package com.dojoapps.mfu;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import org.w3c.dom.Text;
import de.hdodenhof.circleimageview.CircleImageView;
public class SettingsActivity extends AppCompatActivity {
private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;
private CircleImageView mDisplayImage;
private TextView mName;
private TextView mStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
mDisplayImage = (CircleImageView) findViewById(R.id. settings_image);
mName = (TextView) findViewById(R.id. settings_display_name);
mStatus = (TextView) findViewById(R.id. settings_status);
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
FirebaseDatabase.getInstance().getReference();
String current_uid = mCurrentUser.getUid();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue().toString();
String image = dataSnapshot.child("image").getValue().toString();
String status = dataSnapshot.child("status").getValue().toString();
String thumb_image = dataSnapshot.child("thumb image").getValue().toString();
mName.setText(name);
mStatus.setText(status);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
Display More
Und hier meine FireBase Datenbank Struktur :
mfumessageforyou
Users
0f1swOIAXGZyq8KwKnKOPHdZu6K2
image: "default"
name: "445566988"
status: "Hi there, I'm using MFU Chat App."
thumb_image: "default"
Der Fehler liegt wohl ( so vermute ich ) daran, dass die UID von firebase nicht gelesen werden kann -> NullException. Allerdings blieben bisher alle Versuche den Fehler zu fixen erfolglos und ich stehe wirklich vor einem Rätsel.
Ich würde mich riesig freuen, wenn sich jemand diesem Problem annehmen und mir etwas auf die Sprünge helfen könnte
Liebe Grüße
Ebi