即使在发送邮件后,Firebase DB也不会更新

时间:2018-02-03 13:21:06

标签: android firebase firebase-realtime-database

即使在发送邮件后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字段中输入并按send,以便消息可以存储在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("");
            }
        });

2 个答案:

答案 0 :(得分:3)

您需要使用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("");
            }
        });

答案 1 :(得分:0)

我认为问题出在依赖关系中,通过替换来解决:

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'