gpt4 book ai didi

java - 为什么我的应用程序在尝试从 Firebase 检索数据时崩溃?

转载 作者:行者123 更新时间:2023-11-29 22:54:50 25 4
gpt4 key购买 nike

我是 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..."

安卓工作室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

最佳答案

enter image description here

确保在 Firebase 数据库的“规则”部分中,“读”和“写”权限为真,如果不是,则将其设置为真。在此之前,您通过检查 dataSnapshot 是否为空来获取您的值。如下所示:

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 数据库的“规则”部分中的权限。

关于java - 为什么我的应用程序在尝试从 Firebase 检索数据时崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57537216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com