【问题标题】:The Firebase DB don't get updated even after sending messagesFirebase 数据库即使在发送消息后也不会更新
【发布时间】:2018-02-03 13:21:06
【问题描述】:

Firebase 数据库即使在发送消息后也没有得到更新,我得到的错误是:

   30165/? A/FirebaseApp: Firebase API initialization failure.
                                                    java.lang.reflect.InvocationTargetException
                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                        at com.google.firebase.FirebaseApp.zza(Unknown Source)
                                                        at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
                                                        at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
                                                        at com.google.firebase.FirebaseApp.zzbu(Unknown Source)
                                                        at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source)
                                                        at android.content.ContentProvider.attachInfo(ContentProvider.java:1730)
                                                        at android.content.ContentProvider.attachInfo(ContentProvider.java:1705)
                                                        at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
                                                        at android.app.ActivityThread.installProvider(ActivityThread.java:5116)
                                                        at android.app.ActivityThread.installContentProviders(ActivityThread.java:4700)
                                                        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4640)
                                                        at android.app.ActivityThread.access$1500(ActivityThread.java:156)
                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                        at android.os.Looper.loop(Looper.java:211)
                                                        at android.app.ActivityThread.main(ActivityThread.java:5371)
                                                        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:945)
                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)
                                                     Caused by: java.lang.IncompatibleClassChangeError: The method 'java.io.File android.support.v4.content.ContextCompat.getNoBackupFilesDir(android.content.Context)' was expected to be of type virtual but instead was found to be of type direct (declaration of 'java.lang.reflect.ArtMethod' appears in /system/framework/core-libart.jar)
                                                        at com.google.firebase.iid.zzg.zzeC(Unknown Source)
                                                        at com.google.firebase.iid.zzg.<init>(Unknown Source)
                                                        at com.google.firebase.iid.zzg.<init>(Unknown Source)
                                                        at com.google.firebase.iid.zzd.zzb(Unknown Source)
                                                        at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source)
                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                        at java.lang.reflect.Method.invoke(Method.java:372) 
                                                        at com.google.firebase.FirebaseApp.zza(Unknown Source) 
                                                        at com.google.firebase.FirebaseApp.initializeApp(Unknown Source) 
                                                        at com.google.firebase.FirebaseApp.initializeApp(Unknown Source) 
                                                        at com.google.firebase.FirebaseApp.zzbu(Unknown Source) 
                                                        at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source) 
                                                        at android.content.ContentProvider.attachInfo(ContentProvider.java:1730) 
                                                        at android.content.ContentProvider.attachInfo(ContentProvider.java:1705) 
                                                        at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source) 
                                                        at android.app.ActivityThread.installProvider(ActivityThread.java:5116) 
                                                        at android.app.ActivityThread.installContentProviders(ActivityThread.java:4700) 
                                                        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4640) 
                                                        at android.app.ActivityThread.access$1500(ActivityThread.java:156) 
                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412) 
                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                        at android.os.Looper.loop(Looper.java:211) 
                                                        at android.app.ActivityThread.main(ActivityThread.java:5371) 
                                                        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:945) 
                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740) 
    02-03 17:55:18.351 30165-30209/com.google.firebase.udacity.friendlychat E/FA: Task exception on worker thread: java.lang.IncompatibleClassChangeError: The method 'java.io.File android.support.v4.content.ContextCompat.getNoBackupFilesDir(android.content.Context)' was expected to be of type virtual but instead was found to be of type direct (declaration of 'java.lang.reflect.ArtMethod' appears in /system/framework/core-libart.jar): com.google.android.gms.measurement.internal.zzt.zzEd(Unknown Source)

我已经把数据库的规则改成:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

还有我项目中的依赖:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'

    compile 'com.android.support:design:24.2.0'
    compile 'com.android.support:appcompat-v7:24.2.0'

    // Displaying images
    compile 'com.github.bumptech.glide:glide:3.6.1'

    compile 'com.google.firebase:firebase-core:9.0.0'
    compile 'com.google.firebase:firebase-database:9.0.0'
    apply plugin: 'com.google.gms.google-services'
}

该代码应该允许用户在 EditText 字段中写入并按发送,以便消息可以存储在 firebase DB 中。:

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mUsername = ANONYMOUS;

        mFirebaseDatabase =  FirebaseDatabase.getInstance();

        mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("messages");

        // Initialize references to views
        mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
        mMessageListView = (ListView) findViewById(R.id.messageListView);
        mPhotoPickerButton = (ImageButton) findViewById(R.id.photoPickerButton);
        mMessageEditText = (EditText) findViewById(R.id.messageEditText);
        mSendButton = (Button) findViewById(R.id.sendButton);

        // Send button sends a message and clears the EditText
        mSendButton.setOnClickListener(new View.OnClickListener() {


        @Override
            public void onClick(View view) {

                FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, null);
mMessagesDatabaseReference.push().setValue(friendlyMessage);
                // Clear input box
                mMessageEditText.setText("");
            }
        });

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    您需要使用setValue 在 Firebase 数据库中设置消息。

    mSendButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, null);
    
                    mMessagesDatabaseReference.push().setValue(friendlyMessage);
                    // Clear input box
                    mMessageEditText.setText("");
                }
            });
    

    【讨论】:

    • 非常感谢,我可能不小心删除了它,但仍然出现错误(我已编辑问题的详细信息)。
    【解决方案2】:

    我认为问题出在依赖项中,通过替换解决了:

    compile 'com.google.firebase:firebase-core:9.0.0'
    compile 'com.google.firebase:firebase-database:9.0.0'
    

    作者:

    compile 'com.google.firebase:firebase-core:11.8.0'
    compile 'com.google.firebase:firebase-database:11.8.0'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      • 2020-03-06
      • 2020-12-22
      • 2013-06-16
      • 1970-01-01
      相关资源
      最近更新 更多