gpt4 book ai didi

java - 检查 Firebase 实时数据库中的值是否等于字符串

转载 作者:行者123 更新时间:2023-12-02 01:40:43 28 4
gpt4 key购买 nike

当用户登录我的应用程序时,我想检查用户类型是“客户”还是“ field 所有者”。这决定了接下来运行哪个 Activity 。目前,应用程序在尝试登录时崩溃。

这是我的 Firebase Realtime 数据库结构:

two muppets

相关代码如下:

public void loginUser(View v)
{
if(e1.getText().toString().equals("") && e2.getText().toString().equals(""))
{
Toast.makeText(getApplicationContext(),"Blank fields not allowed", Toast.LENGTH_SHORT).show();
}
else
{
auth.signInWithEmailAndPassword(e1.getText().toString(),e2.getText().toString())
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
Toast.makeText(getApplicationContext(),"User logged in successfully",Toast.LENGTH_SHORT).show();
finish();


//Intent i = new Intent(getApplicationContext(),ProfileActivity.class);
//startActivity(i);
DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference();
mDatabaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
//String userType = dataSnapshot.getValue(String.class);

String userType = (String) dataSnapshot.child("userType").getValue();
if (userType.equals("Customer")) {
Intent i = new Intent(getApplicationContext(), UserMainPageActivity.class);
startActivity(i);

} else {
Intent i = new Intent(getApplicationContext(), ProfileActivity.class);
startActivity(i);
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}
else
{
Toast.makeText(getApplicationContext(),"User could not be logged in",Toast.LENGTH_SHORT).show();
}
}
});
}
}

根据要求,这里是崩溃日志:

01-31 13:35:14.348  5322  5322 E AndroidRuntime: FATAL EXCEPTION: main
01-31 13:35:14.348 5322 5322 E AndroidRuntime: Process:
com.example.myapplication, PID: 5322
01-31 13:35:14.348 5322 5322 E AndroidRuntime:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean
java.lang.String.equals(java.lang.Object)' on a null object reference
01-31 13:35:14.348 5322 5322 E AndroidRuntime: at
com.example.myapplication.LoginActivity$1$1.onDataChange(
LoginActivity.java:66 01-31 13:35:14.348 5322 5322 E AndroidRuntime:
at com.google.firebase.database.zzp.onDataChange(Unknown Source)
01-31 13:35:14.348 5322 5322 E AndroidRuntime: at
com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
01-31 13:35:14.348 5322 5322 E AndroidRuntime: at
com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
01-31 13:35:14.348 5322 5322 E AndroidRuntime: at
com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
01-31 13:35:14.348 5322 5322 E AndroidRuntime: at
android.os.Handler.handleCallback(Handler.java:746)
01-31 13:35:14.348 5322 5322 E AndroidRuntime: at
android.os.Handler.dispatchMessage(Handler.java:95)
01-31 13:35:14.348 5322 5322 E AndroidRuntime: at
android.os.Looper.loop(Looper.java:148)
01-31 13:35:14.348 5322 5322 E AndroidRuntime: at
android.app.ActivityThread.main(ActivityThread.java:5443)
01-31 13:35:14.348 5322 5322 E AndroidRuntime: at
java.lang.reflect.Method.invoke(Native Method)
01-31 13:35:14.348 5322 5322 E AndroidRuntime: at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:728)
01-31 13:35:14.348 5322 5322 E AndroidRuntime: at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

这是整个类(class):

    package com.example.myapplication;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
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;

public class LoginActivity extends AppCompatActivity {

EditText e1,e2;

FirebaseAuth auth;

private static final String TAG = "MainActivity";

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

e1 = (EditText)findViewById(R.id.editText);
e2 = (EditText)findViewById(R.id.editText2);

auth = FirebaseAuth.getInstance();
}

public void loginUser(View v)
{
if(e1.getText().toString().equals("") && e2.getText().toString().equals(""))
{
Toast.makeText(getApplicationContext(),"Blank fields not allowed", Toast.LENGTH_SHORT).show();
}
else
{
auth.signInWithEmailAndPassword(e1.getText().toString(),e2.getText().toString())
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
Toast.makeText(getApplicationContext(),"User logged in successfully",Toast.LENGTH_SHORT).show();
finish();

//Intent i = new Intent(getApplicationContext(),ProfileActivity.class);
//startActivity(i);

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child(uid);
uidRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child("userType").equals("Customer")) {
startActivity(new Intent(getApplicationContext(), UserMainPageActivity.class));
} else if (dataSnapshot.child("userType").equals("Venue Owner")) {
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
});
}
else
{
Toast.makeText(getApplicationContext(),"User could not be logged in",Toast.LENGTH_SHORT).show();
}
}
});
}
}

}

最佳答案

要解决此问题,请使用以下代码行:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child("userType").getValue(String.class).equals("Customer")) {
startActivity(new Intent(getApplicationContext(), UserMainPageActivity.class));
} else if (dataSnapshot.child("userType").getValue(String.class).equals("Venue Owner")) {
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

使用此代码,用户将被重定向到相应的 Activity 。

关于java - 检查 Firebase 实时数据库中的值是否等于字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54461377/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com