从firebase数据库获取引用时,应用程序崩溃。以下是我的代码。
在我的app build.gradle文件中添加了这一行
compile 'com.google.firebase:firebase-database:11.0.4'
在Application class
中添加了这一行 FirebaseDatabase.getInstance().setPersistenceEnabled(true);
此行添加在我的Activity onCreate()
中 if(mDatabase == null) {
mDatabase = FirebaseDatabase.getInstance();
mStatus = mDatabase.getReference("status"); //App crashes in this line
}
我的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
buildConfigField "String", "BASE_URL", "\"baseurl\""
applicationId "com.mydomain"
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
jackOptions {
enabled true
}
// Enabling multidex support.
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.google.android.gms:play-services-maps:11.0.4'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.3.1'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.google.android.gms:play-services-location:11.0.4'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
compile 'com.google.firebase:firebase-database:11.0.4'
testCompile 'junit:junit:4.12'
compile 'com.android.support:multidex:1.0.1'
}
apply plugin: 'com.google.gms.google-services'
错误日志
ava.lang.NoSuchMethodError: No direct method <init>(Lcom/google/android/gms/internal/ol;Lcom/google/android/gms/internal/wn;Ljava/util/List;ZLjava/lang/String;Ljava/lang/String;)V in class Lcom/google/firebase/database/connection/idl/zzc; or its super classes (declaration of 'com.google.firebase.database.connection.idl.zzc' appears in /data/app/com.mydomain-1/base.apk)
at com.google.android.gms.internal.mx.zza(Unknown Source)
at com.google.android.gms.internal.qd.zza(Unknown Source)
at com.google.android.gms.internal.qu.<init>(Unknown Source)
at com.google.android.gms.internal.ry.zzb(Unknown Source)
at com.google.android.gms.internal.ry.zza(Unknown Source)
at com.google.firebase.database.FirebaseDatabase.zzFm(Unknown Source)
at com.google.firebase.database.FirebaseDatabase.getReference(Unknown Source)
at com.mydomain.Ui.SplashActivity$3.onResponse(SplashActivity.java:115)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
答案 0 :(得分:0)
<强>更新强> 以下是我用于我的应用的Firebase的依赖项:
compile 'com.google.firebase:firebase-core:10.2.0'
compile 'com.google.firebase:firebase-auth:10.2.0'
compile 'com.google.android.gms:play-services-auth:10.2.0'
compile 'com.google.firebase:firebase-database:10.2.0'
compile 'com.google.firebase:firebase-messaging:10.2.0'
我认为您需要再添加一个依赖项,即:
compile 'com.google.firebase:firebase-core:11.0.4'
请尝试使用此功能。希望它可以帮到你。
答案 1 :(得分:0)
尝试从Tools-&gt; Android-&gt; SDK管理器更新构建工具,而不是更新到可以使用的最新构建工具!