无法将数据保存到Firebase

时间:2020-04-15 19:36:20

标签: android firebase firebase-realtime-database

我正在尝试将其保存到Firebase数据库中,但无法执行。在将应用程序初始化为服务之后,就将其断开,但是应用程序不会崩溃。我可以在页面之间切换,但是无法将数据保存到数据库中。

PS。:在将材质设计应用于应用程序之前,我能够将数据保存到Firebase数据库中。

我正在使用:

  • Java Android
  • Android(Java)的材料设计

我尝试过的事情:

  • 将启动画面中的启动图像从可绘制切换到xxx-hpi mipmap
  • 从项目中删除缓存;
  • 验证了项目的大小,但不是很大,因为我仍未在项目中使用片段或任何非常复杂的项目;
  • 卸载应用程序,然后重新安装

Firebase已在应用程序中连接,规则为:true

Firebase Connection

Firebase Rules

activity_register_user.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RegisterUserActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <ImageView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:contentDescription="@string/txt_description_image"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="32dp"
            android:layout_marginBottom="16dp"
            android:src="@mipmap/ic_icon_round"
            >
        </ImageView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="20dp"
            android:text="@string/register_title"
            android:textAllCaps="true"
            android:textSize="16sp"
            >
        </TextView>

        <!-- ========= REGISTER RADIO BUTTON TYPE USER ========= -->
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.google.android.material.radiobutton.MaterialRadioButton
                    android:id="@+id/radio_btn_register_user"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/radio_btn_user"
                    android:textSize="10pt"
                    android:layout_marginLeft="35sp"
                    android:layout_marginRight="15sp"
                    />

                <com.google.android.material.radiobutton.MaterialRadioButton
                    android:id="@+id/radio_btn_register_worker"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/radio_btn_worker"
                    android:textSize="10pt"
                    android:layout_marginRight="20sp"
                    />
            </LinearLayout>
        </RadioGroup>

        <!-- ========= REGISTER NAME ========= -->
        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/register_username"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="4dp"
            android:hint="@string/register_username"
            app:endIconMode="clear_text"
            >

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/register_username_edit_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="textNoSuggestions"
                android:maxLines="1"
                android:maxLength="15"
                />

        </com.google.android.material.textfield.TextInputLayout>

        <!-- ========= REGISTER SURNAME ========= -->
        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/register_surname"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="4dp"
            android:hint="@string/register_surname"
            app:endIconMode="clear_text"
            >

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/register_surname_edit_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="textNoSuggestions"
                android:maxLines="1"
                android:maxLength="15"
                />

        </com.google.android.material.textfield.TextInputLayout>

        <!-- ========= REGISTER EMAIL ========= -->
        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/regiter_email"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="4dp"
            android:hint="@string/register_email"
            app:endIconMode="clear_text"
            >

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/register_email_edit_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="textEmailAddress|textNoSuggestions"
                android:maxLines="1"
                />

        </com.google.android.material.textfield.TextInputLayout>

        <!-- ========= REGISTER PHONE ========= -->
        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/register_phone"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="@string/register_phone"
            app:endIconMode="clear_text"
            >

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/register_phone_edit_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="phone|textNoSuggestions"
                android:maxLines="1"
                android:maxLength="11"
                />

        </com.google.android.material.textfield.TextInputLayout>

        <!-- ========= REGISTER PASSWORD ========= -->
        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/register_password"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="@string/register_password"
            app:endIconMode="password_toggle"
            >

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/register_password_edit_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="textPassword|textNoSuggestions"
                android:maxLines="1"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                />

        </com.google.android.material.textfield.TextInputLayout>

        <!-- ========= REGISTER BUTTON ========= -->
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent">


            <com.google.android.material.button.MaterialButton
                android:id="@+id/btn_register_user"
                style="@style/Widget.MaterialComponents.Button"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:backgroundTint="@color/btn_color"
                android:text="@string/btn_register_user"
                android:textColor="@color/colorPrimary"
                android:layout_alignParentBottom="true"
                />

        </RelativeLayout>

    </LinearLayout>



</ScrollView>

RegisterUserActivity.java

package com.empodera;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class RegisterUserActivity extends AppCompatActivity
{
    View view;

    /* Get firebase database instance */
    private FirebaseDatabase database = FirebaseDatabase.getInstance();

    private RadioGroup user, worker;
    private EditText name, surname, email, phone, password;

    private String txt_type_user, txt_worker, txt_name, txt_surname, txt_email, txt_password, txt_phone;

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

        name = findViewById(R.id.register_username_edit_text);
        surname = findViewById(R.id.register_surname_edit_text);
        email = findViewById(R.id.register_email_edit_text);
        password = findViewById(R.id.register_password_edit_text);
        phone = findViewById(R.id.register_phone_edit_text);
    }

    public void registerUser(View view)
    {
        String typeUser = "";
        UserApp userApp = new UserApp();
        DatabaseReference databaseReferenceInstance = database.getReference();
        DatabaseReference databaseReference = databaseReferenceInstance.child("user");

        /* VALIDATE IF RADIO BUTTON IS CHECKED
        if(typeUser)
        {
        }
        */

        /* Converting EditText type into String type */
        txt_name = name.getText().toString();
        txt_surname = surname.getText().toString();
        txt_email = email.getText().toString();
        txt_password = password.getText().toString();
        txt_phone = phone.getText().toString();

        /* Select the database instance child user */
        databaseReference = database.getReference("user");

        /* Setting data into ServiceApp class */
        userApp.setName(txt_name);
        userApp.setSurname(txt_surname);
        userApp.setEmail(txt_email);
        userApp.setPhone(txt_phone);
        userApp.setPassword(txt_password);

        /* Transfering data into Firebase object reference */
        userApp.setId(databaseReference.push().getKey());

        /* Data ransfered from object to Firebase database */
        databaseReference.child(userApp.getId()).setValue(userApp);

        Intent activity = new Intent(RegisterUserActivity.this, LoginActivity.class);
        startActivity(activity);
        Toast.makeText(this,"Usuário Cadastrado com Sucesso",Toast.LENGTH_LONG).show();

    }
}

UserApp.java

package com.empodera;

public class UserApp
{
    private String id;
    private String type_user, type_worker;
    private String name, surname,  email, phone, password;

    public UserApp()
    {
        this.id = id;
        this.type_user = type_user;
        this.type_worker = type_worker;
        this.name = name;
        this.surname = surname;
        this.email = email;
        this.phone = phone;
        this.password = password;
    }

    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public String getType_user()
    {
        return type_user;
    }

    public void setType_user(String type_user)
    {
        this.type_user = type_user;
    }

    public String getType_worker()
    {
        return type_worker;
    }

    public void setType_worker(String type_worker)
    {
        this.type_worker = type_worker;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getSurname()
    {
        return surname;
    }

    public void setSurname(String surname)
    {
        this.surname = surname;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone)
    {
        this.phone = phone;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }
}

Logcat

2020-04-15 16:16:11.107 27413-27413/? I/com.empodera: Late-enabling -Xcheck:jni
2020-04-15 16:16:11.483 27413-27413/com.empodera I/com.empodera: The ClassLoaderContext is a special shared library.
2020-04-15 16:16:12.242 27413-27413/com.empodera V/FA: Registered activity lifecycle callback
2020-04-15 16:16:12.245 27413-27413/com.empodera I/FirebaseInitProvider: FirebaseApp initialization successful
2020-04-15 16:16:12.415 27413-27413/com.empodera W/com.empodera: Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
2020-04-15 16:16:12.415 27413-27413/com.empodera W/com.empodera: Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking)
2020-04-15 16:16:12.415 27413-27413/com.empodera W/com.empodera: Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking)
2020-04-15 16:16:12.415 27413-27413/com.empodera W/com.empodera: Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking)
2020-04-15 16:16:12.415 27413-27413/com.empodera W/com.empodera: Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking)
2020-04-15 16:16:12.425 27413-27413/com.empodera V/FA: onActivityCreated
2020-04-15 16:16:12.456 27413-27465/com.empodera V/FA: App measurement collection enabled
2020-04-15 16:16:12.461 27413-27465/com.empodera V/FA: App measurement enabled for app package, google app id: com.empodera, 1:920737313471:android:0d0da5b25ee90997160c0a
2020-04-15 16:16:12.468 27413-27465/com.empodera I/FA: App measurement initialized, version: 21028
2020-04-15 16:16:12.468 27413-27465/com.empodera I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
2020-04-15 16:16:12.469 27413-27465/com.empodera I/FA: To enable faster debug mode event logging run:
      adb shell setprop debug.firebase.analytics.app com.empodera
2020-04-15 16:16:12.469 27413-27465/com.empodera D/FA: Debug-level message logging enabled
2020-04-15 16:16:12.562 27413-27413/com.empodera W/com.empodera: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2020-04-15 16:16:12.565 27413-27413/com.empodera W/com.empodera: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2020-04-15 16:16:12.648 27413-27413/com.empodera D/OpenGLRenderer: Skia GL Pipeline
2020-04-15 16:16:12.755 27413-27465/com.empodera V/FA: Connecting to remote service
2020-04-15 16:16:12.776 27413-27465/com.empodera V/FA: Detected application was in foreground
2020-04-15 16:16:12.785 27413-27468/com.empodera I/Adreno: QUALCOMM build                   : 12b5963, I6fd668c4d3
    Build Date                       : 10/04/18
    OpenGL ES Shader Compiler Version: EV031.25.03.01
    Local Branch                     : 
    Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.7.2.R1.09.00.00.442.049
    Remote Branch                    : NONE
    Reconstruct Branch               : NOTHING
2020-04-15 16:16:12.785 27413-27468/com.empodera I/Adreno: Build Config                     : S L 6.0.7 AArch64
2020-04-15 16:16:12.786 27413-27468/com.empodera D/vndksupport: Loading /vendor/lib64/hw/gralloc.sdm660.so from current namespace instead of sphal namespace.
2020-04-15 16:16:12.790 27413-27465/com.empodera V/FA: Session started, time: 860356701
2020-04-15 16:16:12.806 27413-27468/com.empodera I/Adreno: PFP: 0x005ff112, ME: 0x005ff066
2020-04-15 16:16:12.820 27413-27468/com.empodera I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2020-04-15 16:16:12.821 27413-27468/com.empodera I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2020-04-15 16:16:12.822 27413-27468/com.empodera I/OpenGLRenderer: Initialized EGL, version 1.4
2020-04-15 16:16:12.822 27413-27468/com.empodera D/OpenGLRenderer: Swap behavior 2
2020-04-15 16:16:12.852 27413-27468/com.empodera D/vndksupport: Loading /vendor/lib64/hw/android.hardware.graphics.mapper@2.0-impl.so from current namespace instead of sphal namespace.
2020-04-15 16:16:12.854 27413-27468/com.empodera D/vndksupport: Loading /vendor/lib64/hw/gralloc.sdm660.so from current namespace instead of sphal namespace.
2020-04-15 16:16:12.873 27413-27465/com.empodera V/FA: Connection attempt already in progress
2020-04-15 16:16:12.892 27413-27465/com.empodera I/FA: Tag Manager is not found and thus will not be used
2020-04-15 16:16:12.977 27413-27465/com.empodera V/FA: Connection attempt already in progress
2020-04-15 16:16:13.009 27413-27465/com.empodera V/FA: Connection attempt already in progress
2020-04-15 16:16:13.015 27413-27465/com.empodera V/FA: Activity resumed, time: 860356541
2020-04-15 16:16:13.171 27413-27465/com.empodera V/FA: Connection attempt already in progress
2020-04-15 16:16:13.172 27413-27465/com.empodera V/FA: Connection attempt already in progress
2020-04-15 16:16:13.173 27413-27465/com.empodera D/FA: Connected to remote service
2020-04-15 16:16:13.176 27413-27465/com.empodera V/FA: Processing queued up service tasks: 6
2020-04-15 16:16:17.640 27413-27465/com.empodera V/FA: Recording user engagement, ms: 5005
2020-04-15 16:16:17.648 27413-27465/com.empodera V/FA: Activity paused, time: 860361547
2020-04-15 16:16:17.652 27413-27413/com.empodera W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@a248765
2020-04-15 16:16:17.672 27413-27413/com.empodera V/FA: onActivityCreated
2020-04-15 16:16:17.754 27413-27413/com.empodera W/com.empodera: Accessing hidden method Landroid/widget/TextView;->getTextDirectionHeuristic()Landroid/text/TextDirectionHeuristic; (light greylist, linking)
2020-04-15 16:16:18.095 27413-27465/com.empodera V/FA: Activity resumed, time: 860362006
2020-04-15 16:16:21.441 27413-27465/com.empodera V/FA: Recording user engagement, ms: 3341
2020-04-15 16:16:21.448 27413-27465/com.empodera V/FA: Activity paused, time: 860365347
2020-04-15 16:16:21.450 27413-27413/com.empodera W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@85670f6
2020-04-15 16:16:21.520 27413-27413/com.empodera V/FA: onActivityCreated
2020-04-15 16:16:21.881 27413-27465/com.empodera V/FA: Activity resumed, time: 860365792
2020-04-15 16:16:27.024 27413-27465/com.empodera V/FA: Inactivity, disconnecting from the service
2020-04-15 16:18:42.347 27413-28005/com.empodera V/FA: Recording user engagement, ms: 140370
2020-04-15 16:18:42.360 27413-28005/com.empodera V/FA: Connecting to remote service
2020-04-15 16:18:42.409 27413-28005/com.empodera V/FA: Activity paused, time: 860506176
2020-04-15 16:18:42.569 27413-28005/com.empodera V/FA: Connection attempt already in progress
2020-04-15 16:18:42.570 27413-28005/com.empodera D/FA: Connected to remote service
2020-04-15 16:18:42.573 27413-28005/com.empodera V/FA: Processing queued up service tasks: 2
2020-04-15 16:18:44.437 27413-28005/com.empodera D/FA: Application going to the background
2020-04-15 16:18:49.620 27413-28005/com.empodera V/FA: Inactivity, disconnecting from the service

1 个答案:

答案 0 :(得分:0)

在模型类中使用Empty构造函数。 像这样

package com.empodera;

public class UserApp
{
private String id;
private String type_user, type_worker;
private String name, surname,  email, phone, password;

public UserApp()
{
    this.id = id;
    this.type_user = type_user;
    this.type_worker = type_worker;
    this.name = name;
    this.surname = surname;
    this.email = email;
    this.phone = phone;
    this.password = password;
}
public UserApp(){}//Like this

public String getId()
{
    return id;
}

public void setId(String id)
{
    this.id = id;
}

public String getType_user()
{
    return type_user;
}

public void setType_user(String type_user)
{
    this.type_user = type_user;
}

public String getType_worker()
{
    return type_worker;
}

public void setType_worker(String type_worker)
{
    this.type_worker = type_worker;
}

public String getName()
{
    return name;
}

public void setName(String name)
{
    this.name = name;
}

public String getSurname()
{
    return surname;
}

public void setSurname(String surname)
{
    this.surname = surname;
}

public String getEmail()
{
    return email;
}

public void setEmail(String email)
{
    this.email = email;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone)
{
    this.phone = phone;
}

public String getPassword()
{
    return password;
}

public void setPassword(String password)
{
    this.password = password;
}
}