Firestore RecyclerView 应用程序在运行时崩溃

Posted

技术标签:

【中文标题】Firestore RecyclerView 应用程序在运行时崩溃【英文标题】:Firestore RecyclerView app's crashing when run 【发布时间】:2021-10-17 22:45:33 【问题描述】:

您好,我刚刚完成了我的代码并准备对其进行测试。运行时,它可以正常编译,但在打开应用程序时会崩溃。请问有谁知道我该如何解决这个问题?

我正在尝试制作一个 RecyclerView 以显示来自相应帖子的 cmets,并从 Firebase Firestore 中检索它。

我实现了 FirestoreRecyclerAdapter,这是我的 CommentsActivity:

public class CommentsActivity extends AppCompatActivity 

private FirebaseFirestore firebaseFirestore;
private RecyclerView mFirestoreList;
private Toolbar toolbar;

private FirestoreRecyclerAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_comments);

    toolbar = findViewById(R.id.newpost_toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    firebaseFirestore = firebaseFirestore.getInstance();
    mFirestoreList = findViewById(R.id.comments_List);

    //Query
    Query query = firebaseFirestore.collection("Posts");
    //Recycler Options
    FirestoreRecyclerOptions<BlogPost> options = new FirestoreRecyclerOptions.Builder<BlogPost>()
            .setQuery(query, BlogPost.class)
            .build();

    adapter = new FirestoreRecyclerAdapter<BlogPost, BlogViewHolder>(options) 
        @NonNull
        @Override
        public BlogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comments_list_item, parent, false);
            return new BlogViewHolder(view);
        

        @Override
        protected void onBindViewHolder(@NonNull BlogViewHolder holder, int position, @NonNull BlogPost model) 
            holder.username.setText(model.getUser_id());
            holder.comment.setText(model.getComment());
            holder.userImg.setImageURI(Uri.parse(model.image));
        
    ;

    mFirestoreList.setHasFixedSize(true);
    mFirestoreList.setLayoutManager(new LinearLayoutManager(this));
    mFirestoreList.setAdapter(adapter);



@Override
public boolean onOptionsItemSelected(MenuItem item) 
    switch (item.getItemId()) 
        case android.R.id.home:
            onBackPressed();
            return true;
    
    return super.onOptionsItemSelected(item);


private class BlogViewHolder extends RecyclerView.ViewHolder 

    private TextView username;
    private TextView comment;
    private ImageView userImg;

    public BlogViewHolder(@NonNull View itemView) 
        super(itemView);

        username = itemView.findViewById(R.id.comment_detail_username);
        userImg = itemView.findViewById(R.id.comment_detail_user_img);
        comment = itemView.findViewById(R.id.comment_detail_text);

    


@Override
protected void onStop() 
    super.onStop();
    adapter.stopListening();


@Override
protected void onStart() 
    super.onStart();
    adapter.startListening();

我的博客帖子(模型):

public class BlogPost extends BlogPostID 

public String user_id;
public String image;
public String title;
public String desc;
public String thumbnail;
public String comment;
public Date timestamp;

public BlogPost()

public BlogPost(String user_id, String image, String title, String desc, String thumbnail, String comment, Timestamp timestamp) 
    this.user_id = user_id;
    this.image = image;
    this.title = title;
    this.desc = desc;
    this.thumbnail = thumbnail;
    this.comment = comment;
    this.timestamp = timestamp;





public String getUser_id() 
    return user_id;


public void setUser_id(String user_id) 
    this.user_id = user_id;


public String getImage() 
    return image;


public void setImage(String image) 
    this.image = image;


public String getTitle() 
    return title;


public void setTitle(String title) 
    this.title = title;


public String getDesc() 
    return desc;


public void setDesc(String desc) 
    this.desc = desc;


public String getthumbnail() 
    return thumbnail;


public void setthumbnail(String thumbnail) 
    this.thumbnail = thumbnail;


public String getComment() 
    return comment;


public void setComment(String comment) 
    this.comment = comment;


public Date getTimestamp() 
    return timestamp;


public void setTimestamp(Date timestamp) 
    this.timestamp = timestamp;

这是 Logcat:

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/FirebaseApp$IdTokenListenersCountChangedListener;
    at com.google.firebase.auth.zzp.create(Unknown Source:3)
    at com.google.firebase.components.ComponentRuntime.lambda$new$0(com.google.firebase:firebase-components@@16.0.0:69)
    at com.google.firebase.components.ComponentRuntime$$Lambda$1.get(Unknown Source:4)
    at com.google.firebase.components.Lazy.get(com.google.firebase:firebase-components@@16.0.0:53)
    at com.google.firebase.components.ComponentRuntime.initializeEagerComponents(com.google.firebase:firebase-components@@16.0.0:158)
    at com.google.firebase.FirebaseApp.initializeAllApis(com.google.firebase:firebase-common@@19.3.0:563)
    at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@19.3.0:304)
    at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@19.3.0:268)
    at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@19.3.0:253)
    at com.google.firebase.provider.FirebaseInitProvider.onCreate(com.google.firebase:firebase-common@@19.3.0:51)
    at android.content.ContentProvider.attachInfo(ContentProvider.java:2118)
    at android.content.ContentProvider.attachInfo(ContentProvider.java:2092)
    at com.google.firebase.provider.FirebaseInitProvider.attachInfo(com.google.firebase:firebase-common@@19.3.0:45)
    at android.app.ActivityThread.installProvider(ActivityThread.java:7189)
    at android.app.ActivityThread.installContentProviders(ActivityThread.java:6672)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6567)
    at android.app.ActivityThread.access$1400(ActivityThread.java:224)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1887)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7562)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.FirebaseApp$IdTokenListenersCountChangedListener" on path: DexPathList[[zip file "/data/app/com.starboxlabs.blog-QZgPrUKlYwdoP5giIV2iqQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.starboxlabs.blog-QZgPrUKlYwdoP5giIV2iqQ==/lib/arm64, /system/lib64, /system/product/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:230)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at com.google.firebase.auth.zzp.create(Unknown Source:3) 
    at com.google.firebase.components.ComponentRuntime.lambda$new$0(com.google.firebase:firebase-components@@16.0.0:69) 
    at com.google.firebase.components.ComponentRuntime$$Lambda$1.get(Unknown Source:4) 
    at com.google.firebase.components.Lazy.get(com.google.firebase:firebase-components@@16.0.0:53) 
    at com.google.firebase.components.ComponentRuntime.initializeEagerComponents(com.google.firebase:firebase-components@@16.0.0:158) 
    at com.google.firebase.FirebaseApp.initializeAllApis(com.google.firebase:firebase-common@@19.3.0:563) 
    at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@19.3.0:304) 
    at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@19.3.0:268) 
    at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@19.3.0:253) 
    at com.google.firebase.provider.FirebaseInitProvider.onCreate(com.google.firebase:firebase-common@@19.3.0:51) 
    at android.content.ContentProvider.attachInfo(ContentProvider.java:2118) 
    at android.content.ContentProvider.attachInfo(ContentProvider.java:2092) 
    at com.google.firebase.provider.FirebaseInitProvider.attachInfo(com.google.firebase:firebase-common@@19.3.0:45) 
    at android.app.ActivityThread.installProvider(ActivityThread.java:7189) 
    at android.app.ActivityThread.installContentProviders(ActivityThread.java:6672) 
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6567) 
    at android.app.ActivityThread.access$1400(ActivityThread.java:224) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1887) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:224) 
    at android.app.ActivityThread.main(ActivityThread.java:7562) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 

2021-08-15 13:11:44.590 28344-28344/com.starboxlabs.blog E/MQSEventManagerDelegate: failed to get MQSService.

编辑

我的项目级 Gradle 文件: https://pastebin.com/b2nJV4Qx(*** 问题页面的代码输入不正常)。

我的模块级 Gradle 文件:

    repositories 
        maven  url 'https://maven.fabric.io/public' 
    

    dependencies 
        classpath 'io.fabric.tools:gradle:1.+'
    

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories 
    maven  url 'https://maven.fabric.io/public' 



android 
    compileSdkVersion 28
    defaultConfig 
        applicationId "com.starboxlabs.blog"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 2
        vectorDrawables.useSupportLibrary = true
        versionName "1.1.4"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        packagingOptions 
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/license.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/notice.txt'
            exclude 'META-INF/ASL2.0'
            exclude 'META-INF/rxjava.properties'
        
        compileOptions 
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8


        


    
    buildTypes 
        release 
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        
    
    compileOptions 
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    


dependencies 
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'


    implementation 'id.zelory:compressor:2.1.0'
    implementation 'de.hdodenhof:circleimageview:3.0.0'
    implementation 'com.github.markushi:circlebutton:1.1'
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0-beta01'
    implementation 'com.google.firebase:firebase-messaging:17.3.2'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
    implementation 'me.zhanghai.android.materialprogressbar:library:1.6.1'
    api 'com.theartofdev.edmodo:android-image-cropper:2.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.firebase:firebase-auth:16.1.0'
    implementation 'com.google.firebase:firebase-core:16.0.7'
    implementation 'com.google.firebase:firebase-storage:16.0.5'
    implementation 'com.google.firebase:firebase-firestore:18.0.1'
    implementation 'com.firebaseui:firebase-ui-firestore:6.2.0' // FirebaseUI
    implementation 'com.google.firebase:firebase-analytics'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.2-alpha01'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.2-alpha01'
    implementation "com.mikepenz:materialdrawer:6.1.2"
    implementation "androidx.recyclerview:recyclerview:1.0.0"
    implementation "androidx.annotation:annotation:1.0.2"
    implementation 'com.android.support:design:28.0.0'
    implementation "androidx.constraintlayout:constraintlayout:1.1.3"
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.github.whalemare:sheetmenu:1.0'
    implementation 'com.github.jd-alexander:LikeButton:0.2.3'

    // third party dependencies -------------------------------------------------------------------
    implementation 'com.mikhaellopez:circularimageview:3.2.0'          // circle image view
    implementation 'com.thefinestartist:finestwebview:1.2.7'
    implementation 'uk.co.samuelwall:material-tap-target-prompt:2.14.0'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.intuit.sdp:sdp-android:1.0.6'
    implementation 'com.klinkerapps:drag-dismiss-activity:1.6.1'
    compile('com.crashlytics.sdk.android:crashlytics:2.9.9@aar') 
        transitive = true;
    
    implementation 'androidx.viewpager:viewpager:1.0.0'
    implementation 'com.airbnb.android:lottie:3.6.0' // lottie animations
    implementation 'com.zolad:zoominimageview:1.0.0' // Zoom in ImageView
    implementation 'com.google.android.play:core:1.10.0' // In-App Updates
    implementation 'jp.wasabeef:richeditor-android:2.0.0' // Text Editor
    implementation 'org.sufficientlysecure:html-textview:4.0' // Html Text View




apply plugin: 'com.google.gms.google-services'
com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true```

【问题讨论】:

分享你的应用 gradle 文件 firebase 版本似乎有些问题。 @PriyankaRajput 完成 @sak 我更新了问题 尝试将 firestore 版本升级到最新版本。 【参考方案1】:

您需要将 firebase 依赖项更新为最新的。将以下行添加到应用程序级别的 gradle。从所有依赖项中删除所有版本代码,如下所示 -

您可以点击链接Firebase

  implementation platform('com.google.firebase:firebase-bom:28.3.1')
  // When using the BoM, you don't specify versions in Firebase library 
  //dependencies

 // Declare the dependency for the Firebase SDK for Google Analytics
 implementation 'com.google.firebase:firebase-messaging'
 implementation 'com.google.firebase:firebase-auth'
 implementation 'com.google.firebase:firebase-core'
 implementation 'com.google.firebase:firebase-storage'
 implementation 'com.google.firebase:firebase-firestore'
 implementation 'com.firebaseui:firebase-ui-firestore' 

 implementation 'com.google.firebase:firebase-analytics'

【讨论】:

我做到了,但现在我得到了这个:A problem occurred evaluating project ':app'. &gt; No such property: config for class: com.google.gms.googleservices.GoogleServicesPlugin 分享你的项目级gradle 我尝试以另一种方式重做代码,但效果很好,但是您使用 BoM 的技巧很棒!

以上是关于Firestore RecyclerView 应用程序在运行时崩溃的主要内容,如果未能解决你的问题,请参考以下文章

Firestore 数据无法在 RecyclerView 上显示

活动重启后,Firestore RecyclerView 不显示来自 Firestore 数据库的数据

如何为从 Firestore 填充的 RecyclerView 实现过滤器?

在添加或删除的 Firebase Firestore 数据中重复 RecyclerView 项

添加或删除的Firebase Firestore上的重复RecyclerView项

从 Firestore 异步调用填充 RecyclerView