错误:无法访问LifecycleObserver

Tru*_*ted 6 java android

Android的世界。当我尝试运行应用程序时,它会向日志返回此错误

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

我正在使用此库来管理YouTube播放器

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...
Run Code Online (Sandbox Code Playgroud)

这是我的应用程序的build.gradle

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'
Run Code Online (Sandbox Code Playgroud)

San*_*Sur 6

确保将这些添加为 gradle 依赖项:-

 implementation "android.arch.lifecycle:livedata:1.1.1"
 implementation "android.arch.lifecycle:viewmodel:1.1.1"
 implementation "android.arch.lifecycle:extensions:1.1.1"
Run Code Online (Sandbox Code Playgroud)

并且还要确保添加google()到您repositories的项目级别 gradle 中,并且您已连接到互联网!

编辑

对于androidx使用: -

def lifecycle_version = "2.0.0"

// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
Run Code Online (Sandbox Code Playgroud)