Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: cannot access LifecycleObserver

Tags:

java

android

world of Android. While I try to run the app it log return me this error

Error:(51, 26) error: cannot access LifecycleObserver class file for android.arch.lifecycle.LifecycleObserver not found

I'm using this library for manage the YouTube player

public class DirettaActivity extends AppCompatActivity{

    private YouTubePlayerView youTubePlayerView;
    private FullScreenManager fullScreenManager;

    private @Nullable YouTubePlayer initializedYouTubePlayer;


    private String videoIds = "6JYIGclVQdw";

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diretta);

        fullScreenManager = new FullScreenManager(this);

        youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player_view);

        this.getLifecycle().addObserver(youTubePlayerView); //this line give me error! "Cannot resolve getLifecycle"

        youTubePlayerView.initialize(initializedYouTubePlayer -> {

            initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() {
                @Override
                public void onReady() {
                    DirettaActivity.this.initializedYouTubePlayer = initializedYouTubePlayer;

                    initializedYouTubePlayer.loadVideo(videoIds, 0);
                }
            });

            addFullScreenListenerToPlayer(initializedYouTubePlayer);

        }, true);
    } //other code...

Here my build.gradle of the app

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    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:23.3.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.facebook.android:facebook-android-sdk:4.16.0'
    compile 'com.pkmmte.view:circularimageview:1.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.github.yalantis:ucrop:2.2.1'
    compile 'com.android.support:design:23.3.0'
    compile 'com.google.android.gms:play-services-gcm:11.0.4'
    compile "android.arch.lifecycle:runtime:1.0.0-alpha2"
    compile "android.arch.lifecycle:extensions:1.0.0-alpha2"
    implementation "android.arch.lifecycle:livedata:1.1.1"
    annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha2"
    compile 'com.writingminds:FFmpegAndroid:0.3.2'
    compile 'com.google.apis:google-api-services-youtube:v3-rev186-1.23.0'
    compile files('libs/YouTubeAndroidPlayerApi.jar')
    testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
like image 410
Trusted Avatar asked Mar 26 '18 07:03

Trusted


2 Answers

Make sure you have these added as gradle dependencies :-

 implementation "android.arch.lifecycle:livedata:1.1.1"
 implementation "android.arch.lifecycle:viewmodel:1.1.1"
 implementation "android.arch.lifecycle:extensions:1.1.1"

And also make sure to add google() to your repositories in project level gradle and you are connected to internet !!

Edit

For androidx use :-

def lifecycle_version = "2.0.0"

// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
like image 67
Santanu Sur Avatar answered Sep 30 '22 18:09

Santanu Sur


Add these two lines in gradle.properties

android.useAndroidX=true

android.enableJetifier=true

when you add these two lines you will get an error in activity. You need to modify all components from android to androidX

Eg: android.support.v7.app.AppCompatActivity to androidx.appcompat.app.AppCompatActivity

like image 29
mahesh kumar Avatar answered Sep 30 '22 17:09

mahesh kumar