无法解析方法addOnCompleteListener

时间:2018-07-11 10:08:14

标签: java android firebase firebase-cloud-messaging

下面是我程序的一部分。我尝试使用firebase。有点用这种方法有很多问题。

subscribeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "Subscribing to news topic");
            FirebaseMessaging.getInstance().subscribeToTopic("news").addOnCompleteListener(new OnCompleteListener <Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            String msg = getString(R.string.msg_subscribed);
                            if (!task.isSuccessful()) {
                                msg = getString(R.string.msg_subscribe_failed);
                            }
                            Log.d(TAG, msg);
                            Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                        }
                    });
        }
    });

这是错误:

cannot resolve method addOnCompleteListener anonymous com.google.android.gms.tasks.OnCompleteListener<java.lang.Void>

我的build.gradle

dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
//implementation 'com.google.protobuf:protobuf-java:2.6.1'
implementation 'com.google.firebase:firebase-core:11.8.0'
implementation 'com.google.firebase:firebase-messaging:11.8.0'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:2.8.1'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile('com.thoughtworks.xstream:xstream:1.4.7') {
    exclude group: 'xmlpull', module: 'xmlpull'
}
compile 'jp.wasabeef:blurry:2.1.1'
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:cardview-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.crystal:crystalrangeseekbar:1.1.3'
compile 'wzd.anarchy:library:unspecified'
implementation 'com.android.support:support-v4:25.4.0'
compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.8'
testCompile 'junit:junit:4.12'
compile 'com.google.protobuf:protobuf-java:3.0.0-beta-3'
dependencies {
    compile 'com.github.TouchBoarder:weekdays-buttons-bar:v1.0.2'
}
compile 'com.jakewharton:butterknife:8.6.0'
compile 'com.firebase:firebase-jobdispatcher:0.5.2'
implementation 'com.google.firebase:firebase-messaging:17.1.0'
implementation 'com.google.firebase:firebase-core:16.0.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
compile 'com.google.android.gms:play-services:12.0.1'
compile "com.polidea.rxandroidble:rxandroidble:1.4.3"
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'io.reactivex.rxjava2:rxjava:2.1.8'
}


apply plugin: 'com.google.gms.google-services'

如果添加实现,则会看到错误:无法访问zzbgl 找不到com.google.android.gms.internal.zzbgl的类文件

1 个答案:

答案 0 :(得分:1)

由于缺少某些依赖项而导致出现该错误。

FirebaseMessaging.getInstance().subscribeToTopic("news")

返回一个Task<Void>对象。为了在此对象上附加侦听器,您需要向build.gradle文件添加以下依赖项:

implementation 'com.google.firebase:firebase-messaging:17.1.0'
implementation 'com.google.firebase:firebase-core:16.0.1'

根据您的build.gradle文件,您还应该更改以下代码行:

compile 'com.google.android.gms:play-services:12.0.1'

implementation 'com.google.android.gms:play-services-location:15.0.1'
implementation 'com.google.android.gms:play-services-analytics:16.0.1'

也不要忘记添加:

classpath 'com.google.gms:google-services:4.0.1'

在另一个build.gradle文件中。