应用程序连接到Firebase时程序崩溃

时间:2018-06-03 20:16:37

标签: java android firebase firebase-realtime-database

当应用连接到Firebase时,程序崩溃了。我更新了SDK,但它不起作用。

我只有一项活动失败。就是这样:

public class MainActivity extends AppCompatActivity {

    private Button btn_save_data;
    private EditText e_name;
    private FirebaseDatabase firebaseDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e_name = (EditText) findViewById(R.id.name);
        btn_save_data = (Button) findViewById(R.id.save_data);
        btn_save_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = e_name.getText().toString();
                firebaseDatabase = FirebaseDatabase.getInstance();
                DatabaseReference myRef = firebaseDatabase.getReference("users");
                myRef.setValue(name);
            }
        });
    }

}

的build.gradle

 apply plugin: 'com.android.application'

    android {
        compileSdkVersion 26
        defaultConfig {
            applicationId "adais.mywayapp"
            minSdkVersion 15
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:26.1.0'
        implementation 'com.google.firebase:firebase-core:16.0.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.0'
        implementation 'com.google.firebase:firebase-database:11.8.0'
        implementation 'com.google.firebase:firebase-messaging:12.0.1'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }
    apply plugin: 'com.google.gms.google-services'

错误详情:

06-03 23:31:33.272 8908-8908/adais.mywayapp W/dalvikvm: VFY: unable to resolve virtual method 26689: Lcom/google/firebase/FirebaseApp;.zzbqo ()Z
06-03 23:31:33.272 8908-8908/adais.mywayapp D/dalvikvm: VFY: replacing opcode 0x6e at 0x0081
06-03 23:31:33.284 8908-8908/adais.mywayapp D/AndroidRuntime: Shutting down VM
06-03 23:31:33.284 8908-8908/adais.mywayapp W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41874ce0)
    threadid=1: uncaught exception occurred
06-03 23:31:33.288 8908-8908/adais.mywayapp W/System.err: java.lang.NoSuchMethodError: com.google.firebase.FirebaseApp.zzbqo
06-03 23:31:33.293 8908-8908/adais.mywayapp W/System.err:     at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
        at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
        at adais.mywayapp.MainActivity$1.onClick(MainActivity.java:26)
        at android.view.View.performClick(View.java:4463)
        at android.view.View$PerformClick.run(View.java:18789)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
06-03 23:31:33.294 8908-8908/adais.mywayapp W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5299)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
06-03 23:31:33.295 8908-8908/adais.mywayapp W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
        at dalvik.system.NativeStart.main(Native Method)
06-03 23:31:33.295 8908-8908/adais.mywayapp W/dalvikvm: threadid=1: calling UncaughtExceptionHandler
06-03 23:31:33.306 8908-8908/adais.mywayapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: adais.mywayapp, PID: 8908
    java.lang.NoSuchMethodError: com.google.firebase.FirebaseApp.zzbqo
        at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
        at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
        at adais.mywayapp.MainActivity$1.onClick(MainActivity.java:26)
        at android.view.View.performClick(View.java:4463)
        at android.view.View$PerformClick.run(View.java:18789)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5299)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
        at dalvik.system.NativeStart.main(Native Method)
06-03 23:31:34.778 8908-8908/adais.mywayapp I/Process: Sending signal. PID: 8908 SIG: 9

1 个答案:

答案 0 :(得分:3)

您正在使用旧版本和新版本的firebase依赖项,因此请使用此处提及的最新版本

Firebase Android Release Notes

例如使用

dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.google.firebase:firebase-core:16.0.0'
        implementation 'com.google.firebase:firebase-database:16.0.1'
        implementation 'com.google.firebase:firebase-messaging:17.0.1'
        //...other
    }