【发布时间】:2021-07-12 14:27:07
【问题描述】:
我正在学习 java 并尝试在他们在应用程序中注册时在 firebase 实时数据库中注册用户的信息,但是数据没有存储在数据库中,并且 logcat 没有显示错误,所以我不知道是什么问题。
附: - 应用程序连接到数据库和firebase数据库规则是:
{
"rules": {
".read": true,
".write": true
}
}
这是 SignUp.java 文件的代码
package com.example.foodapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class SignUp extends AppCompatActivity {
//variables
private TextInputLayout regName,regUsername,regEmail,regPhoneNo,regPassword;
private Button regBtn,regToLogInBtn;
FirebaseDatabase rootNode;
DatabaseReference reference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
//hooks
regName = (TextInputLayout )findViewById(R.id.reg_name);
regUsername = (TextInputLayout )findViewById(R.id.reg_username);
regEmail = (TextInputLayout )findViewById(R.id.reg_email);
regPhoneNo = (TextInputLayout )findViewById(R.id.reg_phoneNo);
regPassword = (TextInputLayout )findViewById(R.id.reg_password);
regBtn = (Button)findViewById(R.id.reg_btn);
regToLogInBtn = (Button)findViewById(R.id.reg_login_btn);
//save data in firebase using button click
regBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference("users");
Log.d("HELLO", "Register Button Clicked");
//get all value from text fields
String name = regName.getEditText().getText().toString();
String username = regUsername.getEditText().getText().toString();
String email = regEmail.getEditText().getText().toString();
String phoneNo = regPhoneNo.getEditText().getText().toString();
String password = regPassword.getEditText().getText().toString();
UserHelperClass helperClass = new UserHelperClass(name, username, email, phoneNo, password);
reference.child(phoneNo).push().setValue(helperClass);
}
});
}
}
xml 代码是:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout 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=".SignUp"
android:orientation="vertical"
android:background="#fff"
android:padding="20dp">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/logo"
android:transitionName="logo_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome"
android:textSize="40sp"
android:fontFamily="@font/bungee"
android:layout_marginTop="-20dp"
android:textColor="#000"
android:transitionName="logo_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SignUp to start your new Journey"
android:textSize="18sp"
android:transitionName="logo_desc"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/reg_name"
android:hint="Full Name"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
//-----------------------------------------------------------------
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/reg_username"
app:counterMaxLength="15"
android:hint="Username"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:transitionName="username_tran">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
//--------------------------------------------------------------
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/reg_email"
android:hint="Email"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
//-----------------------------------------------------------
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/reg_phoneNo"
android:hint="Phone Number"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
//---------------------------------------------------------
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/reg_password"
android:hint="Password"
app:passwordToggleEnabled="true"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:transitionName="password_tran">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<Button
android:id="@+id/reg_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000"
android:text="GO"
android:textColor="#fff"
android:transitionName="button_tran" />
<Button
android:id="@+id/reg_login_btn"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:text="Already have an account? Login"
android:textColor="#000"
android:transitionName="login_signup_tran" />
</LinearLayout>
</ScrollView>
```
the database is:
[1]: https://i.stack.imgur.com/IlaDs.png
【问题讨论】:
-
您是否尝试将完整的侦听器附加到 setValue() 操作,以查看是否出现问题?
-
UserHelperClass中有公共 getter 吗? -
是的...我在 getter 和 setter 中使用了 public @p2pdops
-
@AlexMamo ...我还没有尝试过完整的监听器...但我已经尝试过..尝试catch块来查看Toast中的错误......但它没有显示错误......
-
不行,尝试附加一个监听器,检查任务是否成功。是否在 logcat 中打印一些内容?
标签: java android firebase kotlin