错误:无法访问LifecycleObserver

8

Android世界。当我尝试运行应用程序时,它会记录返回此错误

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

我正在使用这个来管理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...

这是我应用程序的 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'
2个回答

8

请确保您已将以下内容添加为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"

同时确保在项目级别的gradle中添加google()到你的repositories,并且你已连接到互联网!!

编辑

对于androidx使用:

def lifecycle_version = "2.0.0"

// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"

好像不起作用,我在 build.gradle 中写的是正确的吗? - Trusted
无法解析配置文件中的所有文件':app:debugCompileClasspath'。 找不到android.arch.lifecycle:livedata:1.1.1。 - Trusted
1
请确保您已连接到互联网并禁用了离线工作!!@可信任 - Santanu Sur
1
我将解决将google()添加到依赖项中的问题!但是我遇到了一个新错误,这就是生活!非常感谢您的帮助! - Trusted

2
gradle.properties 中添加以下两行内容:
android.useAndroidX=true

android.enableJetifier=true

当你添加这两行代码后,你将在Activity中遇到一个错误。你需要将所有组件从android修改为androidX

例如:android.support.v7.app.AppCompatActivity 修改为 androidx.appcompat.app.AppCompatActivity

注意:本文档翻译仅供参考,如有与原文不符之处,请以原文为准。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接