我是Firebase的新手,我正尝试从Firebase的实时数据库中检索我的项目的数据,但是只要它能够获取到 onClickFunction应用程序崩溃无法确定原因。我很确定这与Firebase代码有关
我正在从这里关注本教程 https://www.youtube.com/watch?v=LpWhAz3e1sI
我尝试使用实现'com.android.support:multidex:1.0.3',但仍然崩溃
Firebase数据
myfirebase-fbc55
|
|
Movies
|
|
movie1
movieName: "The Angry Birds Movie 2"
plot: "The Angry Birds Movie 2 (also referred to as An..."
movie2
movieName: "Good Boys"
plot: "Good Boys is a 2019 American comedy film co-wri..."
movie3
movieName: "Where'd You Go, Bernadette"
plot: "Where'd You Go, Bernadette is a 2012 comedy nov..."
Android Studio Java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but1 = (Button) findViewById(R.id.button);
but1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Log.d("working", "working ");
db = FirebaseDatabase.getInstance().getReference().child("Movies").child("movie1");
db.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String movie = dataSnapshot.child("movieName").getValue().toString();
String plot = dataSnapshot.child("plot").getValue().toString();
Log.d("movie", movie);
Log.d("plot", plot);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
}
Android Studio Gradle
android {
compileSdkVersion 29
buildToolsVersion "29.0.0"
defaultConfig {
applicationId "com.example.firebasedemo"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-database:16.0.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.firebase:firebase-core:17.0.0'
implementation 'com.android.support:multidex:1.0.3'
}
apply plugin: 'com.google.gms.google-services'
我希望数据至少在movie1中显示在日志中,所以我知道该应用程序正在正确检索数据。
这是错误日志
08-17 14:00:26.316 10148-10148/com.example.firebasedemo I/art: Rejecting re-init on previously-failed class java.lang.Class<com.google.firebase.database.android.AndroidAuthTokenProvider$3>
08-17 14:00:26.317 10148-10148/com.example.firebasedemo I/art: Rejecting re-init on previously-failed class java.lang.Class<com.google.firebase.database.android.AndroidAuthTokenProvider$3>
08-17 14:00:26.374 10148-10230/com.example.firebasedemo I/art: Rejecting re-init on previously-failed class java.lang.Class<com.google.firebase.database.android.AndroidAuthTokenProvider$3>
08-17 14:00:26.376 10148-10230/com.example.firebasedemo E/RunLoop: Uncaught exception in Firebase Database runloop (3.0.0). Please report to firebase-database-client@google.com
java.lang.NoClassDefFoundError: com.google.firebase.database.android.AndroidAuthTokenProvider$3
at com.google.firebase.database.android.AndroidAuthTokenProvider.produceIdTokenListener(com.google.firebase:firebase-database@@16.0.4:85)
at com.google.firebase.database.android.AndroidAuthTokenProvider.addTokenChangeListener(com.google.firebase:firebase-database@@16.0.4:80)
at com.google.firebase.database.core.Repo.deferredInitialization(com.google.firebase:firebase-database@@16.0.4:109)
at com.google.firebase.database.core.Repo.access$000(com.google.firebase:firebase-database@@16.0.4:55)
at com.google.firebase.database.core.Repo$1.run(com.google.firebase:firebase-database@@16.0.4:94)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
08-17 14:00:26.381 10148-10148/com.example.firebasedemo D/AndroidRuntime: Shutting down VM
08-17 14:00:26.382 10148-10148/com.example.firebasedemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.firebasedemo, PID: 10148
java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@2736e3ff rejected from com.google.firebase.database.core.utilities.DefaultRunLoop$1@210f5cc[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2011)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:793)
at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:298)
at java.util.concurrent.ScheduledThreadPoolExecutor.schedule(ScheduledThreadPoolExecutor.java:503)
at java.util.concurrent.ScheduledThreadPoolExecutor.execute(ScheduledThreadPoolExecutor.java:592)
at com.google.firebase.database.core.utilities.DefaultRunLoop.scheduleNow(com.google.firebase:firebase-database@@16.0.4:105)
at com.google.firebase.database.core.Repo.scheduleNow(com.google.firebase:firebase-database@@16.0.4:276)
at com.google.firebase.database.Query.addEventRegistration(com.google.firebase:firebase-database@@16.0.4:233)
at com.google.firebase.database.Query.addValueEventListener(com.google.firebase:firebase-database@@16.0.4:149)
at com.example.firebasedemo.MainActivity$1.onClick(MainActivity.java:36)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
08-17 14:00:28.418 10148-10148/? I/Process: Sending signal. PID: 10148 SIG: 9
答案 0 :(得分:1)
确保在Firebase数据库的“规则”部分中,“读”和“写”权限为true,否则请设置为true。在此之前获取值的位置通过检查dataSnapshot是否为null进行验证。如下所示:
db.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot != null) {
String movie = dataSnapshot.child("movieName").getValue().toString();
String plot = dataSnapshot.child("plot").getValue().toString();
Log.d("movie", movie);
Log.d("plot", plot);
} else {
Log.d(TAG, "Data Snapshot is null");
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
这样做,您将知道您是否正在接收任何数据。并且不要忘记检查Firebase数据库“规则”部分中的权限。