Cannot resolve method addOnCompleteListener

2019-09-05 14:25发布

Part of my program is given below. I try works with firebase. bit I have a lot of problems with this method.

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();
                        }
                    });
        }
    });

This is the error:

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

My 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'

If I add implementations, I see error: cannot access zzbgl class file for com.google.android.gms.internal.zzbgl not found

1条回答
聊天终结者
2楼-- · 2019-09-05 14:38

You are getting that error because you are missing some dependencies.

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

Returns a Task<Void> object. In order to attach a listener on this object, you need to add the following dependencies to build.gradle file:

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

According to your build.gradle file, you also should change this line of code:

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

to

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

And also don't forget to add:

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

In the other build.gradle file.

查看更多
登录 后发表回答