当我使用 firebase auth 注册时,注册成功但注册后 我尝试写一些关于用户的信息,但问题没有写在 firebase 实时数据库中。
<块引用>注册片段
package com.ar.team.company.app.ar_app_25.ui.sign.up;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.ar.team.company.app.ar_app_25.ui.sign.sign.SignActivity;
import com.ar.team.company.app.ar_app_25.databinding.FragmentSignupBinding;
import com.ar.team.company.app.ar_app_25.ui.sign.way.SignWayActivity;
import com.ar.team.company.app.ar_app_25.utils.Utils;
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.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Objects;
@SuppressWarnings({"FieldCanBeLocal", "ConstantConditions"})
public class SignupFragment extends Fragment implements OnCompleteListener<AuthResult> {
// This for control the Fragment-Layout views:
private FragmentSignupBinding binding;
private FirebaseAuth auth;
private FirebaseUser user;
private DatabaseReference reference;
private ProgressDialog progress;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the fragment layout:
binding = FragmentSignupBinding.inflate(inflater, container, false);
return binding.getRoot(); // Get the fragment layout root.
}
@Override
public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Initializing:
auth = FirebaseAuth.getInstance();
progress = Utils.dialog(requireContext(), "Creating your amazing account!", "Please be patient");
// Developing:
binding.signupButton.setOnClickListener(this::signupNewAccount);
}
// This is signup method:
private void signupNewAccount(View view) {
// Getting data:
String userGmail = Objects.requireNonNull(binding.userGmailEditText.getText()).toString();
String userPass = Objects.requireNonNull(binding.userPassEditText.getText()).toString();
// Creating new firebase account:
if (!userGmail.isEmpty() && !userPass.isEmpty() && userGmail != null && userPass != null) {
auth.createUserWithEmailAndPassword(userGmail, userPass).addOnCompleteListener(this);
progress.show();
} else {
Utils.showResult(binding.parentLayout, "Please enter your info", "Close", null);
}
}
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isComplete() && task.isSuccessful()) {
// Initializing:
user = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference(Utils.FIREBASE_MAIN_REFERENCE).child(user.getUid());
// Setting our data:
HashMap<String, String> data = new HashMap<>();
data.put(Utils.FIREBASE_NAME_VALUE, Objects.requireNonNull(binding.userNameEditText.getText()).toString());
data.put(Utils.FIREBASE_IMAGE_VALUE, "default");
// Dismissing the progress:
progress.dismiss();
// Setting the value:
Task<Void> voidTask = reference.setValue(data);
voidTask.addOnCompleteListener(task1 -> {
if (task1.isComplete() && task1.isSuccessful())
Utils.showResult(binding.parentLayout, "Signed Successfully", "LOGIN", this::restartMethod);
});
voidTask.addOnFailureListener(Throwable::printStackTrace);
} else {
progress.hide();
Utils.showResult(binding.parentLayout, "Something wrong!", "Close");
}
}
private void restartMethod(View view) {
// Initializing:
requireActivity().finish();
Intent signIntent = new Intent(requireContext(), SignActivity.class);
// Put extras:
signIntent.putExtra(SignWayActivity.WAY_ACTION, SignWayActivity.WAY_ACTION_IN);
// Developing:
requireActivity().startActivity(signIntent);
}
}
<块引用>
实用类
package com.ar.team.company.app.ar_app_25.utils;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import com.google.android.material.snackbar.Snackbar;
public class Utils {
// Firebase constance:
public static final String FIREBASE_MAIN_REFERENCE = "Users";
public static final String FIREBASE_NAME_VALUE = "name";
public static final String FIREBASE_IMAGE_VALUE = "image";
// Firebase storage constance:
public static final String FIREBASE_STORAGE_IMAGES_LOCATION = "profile_images";
public static final String FIREBASE_STORAGE_IMAGES_URL = "gs://ar-app-25.appspot.com/" + FIREBASE_STORAGE_IMAGES_LOCATION + "/";
// Showing progressDialog:
public static ProgressDialog dialog(Context context, String title, String content) {
// Initializing:
ProgressDialog progress = new ProgressDialog(context);
// Developing:
progress.setTitle(title);
progress.setMessage(content);
progress.setCanceledOnTouchOutside(false);
// Returning:
return progress;
}
// Showing snackBar:
public static void showResult(View view, CharSequence sequence, CharSequence action, View.OnClickListener listener) {
Snackbar snackbar = Snackbar.make(view, sequence, Snackbar.LENGTH_SHORT);
snackbar.setAction(action, listener);
snackbar.show();
}
public static void showResult(View view, CharSequence sequence, CharSequence action) {
Snackbar snackbar = Snackbar.make(view, sequence, Snackbar.LENGTH_SHORT);
snackbar.setAction(action, v -> Log.d("SnackBarAction", "Close"));
snackbar.show();
}
}
<块引用>
Android Studio(运行)
08/02 01:16:29: Launching 'app' on AR-Device.
Install successfully finished in 228 ms.
$ adb shell am start -n "com.ar.team.company.app.ar_app_25/com.ar.team.company.app.ar_app_25.ui.main.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 8807 on device 'AR-Device [emulator-5554]'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/studio.deploy: Finished instrumenting
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
No Network Security Config specified, using platform default
I/FirebaseApp: Device unlocked: initializing all Firebase APIs for app [DEFAULT]
I/DynamiteModule: Considering local module com.google.android.gms.measurement.dynamite:51 and remote module com.google.android.gms.measurement.dynamite:51
Selected remote version of com.google.android.gms.measurement.dynamite, version >= 51
V/DynamiteModule: Dynamite loader version >= 2, using loadModule2NoCrashUtils
D/FirebaseAuth: Notifying id token listeners about user ( XMjhchhlFHWfnzPK06se4Ib2Cjg1 ).
I/FirebaseInitProvider: FirebaseApp initialization successful
D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
I/FirebaseAuth: [FirebaseAuth:] Preparing to create service connection to fallback implementation
V/FA: onActivityCreated
V/FA: App measurement collection enabled
V/FA: App measurement enabled for app package, google app id: com.ar.team.company.app.ar_app_25, 1:405566473108:android:dce00fe1a9ce3130d70581
I/FA: App measurement initialized, version: 42041
To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
To enable faster debug mode event logging run:
adb shell setprop debug.firebase.analytics.app com.ar.team.company.app.ar_app_25
D/FA: Debug-level message logging enabled
W/y.app.ar_app_2: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
V/FA: Connecting to remote service
V/FA: Connection attempt already in progress
V/FA: Connection attempt already in progress
V/FA: Activity resumed, time: 27797530
I/FA: Tag Manager is not found and thus will not be used
D/HostConnection: HostConnection::get() New Host Connection established 0xc6f8f040, tid 8838
D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_async_frame_commands ANDROID_EMU_gles_max_version_2
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/EGL_emulation: eglCreateContext: 0xc6f8e240: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0xc6f8e240: ver 2 0 (tinfo 0xb8284030) (first time)
V/FA: Connection attempt already in progress
V/FA: Connection attempt already in progress
I/Gralloc4: mapper 4.x is not supported
D/HostConnection: createUnique: call
HostConnection::get() New Host Connection established 0xc6f8eef0, tid 8838
D/goldfish-address-space: allocate: Ask for block of size 0x100
allocate: ioctl allocate returned offset 0x387ffe000 size 0x2000
D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_async_frame_commands ANDROID_EMU_gles_max_version_2
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 5
W/System: A resource failed to call close.
V/FA: Recording user engagement, ms: 3007
V/FA: Activity paused, time: 27800538
V/FA: onActivityCreated
V/FA: Activity resumed, time: 27800652
V/FA: Recording user engagement, ms: 1944
V/FA: Activity paused, time: 27802596
V/FA: onActivityCreated
V/FA: Activity resumed, time: 27802943
I/AssistStructure: Flattened final assist data: 3812 bytes, containing 1 windows, 30 views
V/FA: Inactivity, disconnecting from the service
W/System: Ignoring header X-Firebase-Locale because its value was null.
W/System: Ignoring header X-Firebase-Locale because its value was null.
D/FirebaseAuth: Notifying id token listeners about user ( wyVwxXzIKEgcHOMbK9hbc8KP9PY2 ).
Notifying auth state listeners about user ( wyVwxXzIKEgcHOMbK9hbc8KP9PY2 ).
所以我认为问题是我无法访问 firebase 实时数据库 但我对此不太确定,因为这是我第一次使用 firebase
&谢谢大家。