我正在尝试从Firebase读取数据,但应用程序在以下logcat中生成错误时崩溃。我已经尝试了stackoverflow和其他站点上所有可用的解决方案,但仍然无法解决此问题,我进行了更改并在此处共享了必要的代码
.gradle项目(依赖项)
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.google.gms:google-services:4.2.0'
}
app.gradle(依赖项)
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:animated-vector-drawable:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-database:17.0.0'
implementation 'com.google.firebase:firebase-core:16.0.9'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-
core:3.0.2'
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
}
main.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseApp.initializeApp(getApplicationContext());
initViews();
mBtn.setOnClickListener(this::readData);
}
public void readData(View view) {
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String data = dataSnapshot.getValue(String.class);
setT.setText(data);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
setT.setText("Sorry Data");
}
});
}
private void initViews() {
mBtn = findViewById(R.id.mBtn);
setT = findViewById(R.id.setT);
}
}
答案 0 :(得分:0)
尝试将数据库实施版本17.0.0替换为此版本
implementation 'com.google.firebase:firebase-database:16.0.5'
它将起作用