Java Firebase实时数据库不显示添加到其中的值。仅显示null
我已经添加了所有依赖项和所有,但是数据仍然没有显示在firebase realtime上 数据库屏幕。Ot仅显示空值。所以我添加了使用firebase的代码 注册屏幕代码:Java Firebase实时数据库不显示添加到其中的值。仅显示null,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我已经添加了所有依赖项和所有,但是数据仍然没有显示在firebase realtime上 数据库屏幕。Ot仅显示空值。所以我添加了使用firebase的代码 注册屏幕代码: public class SignUp extends AppCompatActivity { TextInputLayout regName, regUsername, regPassword, regEmail, regPhoneno; Button regBtn, regLoginbtn;
public class SignUp extends AppCompatActivity {
TextInputLayout regName, regUsername, regPassword, regEmail, regPhoneno;
Button regBtn, regLoginbtn;
FirebaseDatabase rootNode;
DatabaseReference reference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
regName = findViewById(R.id.name);
regUsername = findViewById(R.id.username);
regPassword = findViewById(R.id.password);
regEmail = findViewById(R.id.email);
regPhoneno = findViewById(R.id.PhoneNo);
regBtn = findViewById(R.id.button);
regLoginbtn = findViewById(R.id.button2);
regBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = Objects.requireNonNull(regName.getEditText()).getText().toString();
String username = Objects.requireNonNull(regUsername.getEditText()).getText().toString();
String password = Objects.requireNonNull(regPassword.getEditText()).getText().toString();
String email = Objects.requireNonNull(regEmail.getEditText()).getText().toString();
String phoneNo = Objects.requireNonNull(regPhoneno.getEditText()).getText().toString();
UserHelperClass helperClass = new UserHelperClass(name, password, username, email, phoneNo);
FirebaseDatabase.getInstance().getReference().child("my").child("users").setValue(helperClass);
}
});
}
}
从应用程序获取数据的类
build.gradle项目:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.1"
classpath "com.google.gms:google-services:4.3.8"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
mavenCentral()
// Warning: this repository is going to shut down soon
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle应用程序:
plugins {
id 'com.android.application'
}
[enter image description here][1]
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.medilyf"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
dependencies {
implementation platform('com.google.firebase:firebase-bom:28.0.1')
implementation 'com.google.firebase:firebase-analytics'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.firebase:firebase-database:20.0.0'
implementation 'org.jetbrains:annotations:15.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.airbnb.android:lottie:3.4.1'
implementation 'androidx.viewpager2:viewpager2:1.0.0'
}
我认为这是因为UserHelperClass对象为空。不要使用重载构造函数来设置值,而是使用模型类的setter。在setValue之后添加一个
OnCompletelistener
,然后查看错误
仅供参考:如果您检查logcat输出以了解
setValue
何时运行,是否有任何错误消息?您是否尝试将侦听器附加到.setValue(helperClass)
操作?你有什么错误吗?如何在我的代码中使用它,但在.setValue(“你的值”)中再添加一个参数,.setValue(“你的值(helperClass)”,新的OnCompleteListener(){//ovveride functions here})
下面是一个简单的例子
plugins {
id 'com.android.application'
}
[enter image description here][1]
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.medilyf"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
dependencies {
implementation platform('com.google.firebase:firebase-bom:28.0.1')
implementation 'com.google.firebase:firebase-analytics'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.firebase:firebase-database:20.0.0'
implementation 'org.jetbrains:annotations:15.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.airbnb.android:lottie:3.4.1'
implementation 'androidx.viewpager2:viewpager2:1.0.0'
}