java - 错误 : cannot access LifecycleObserver

标签 java android

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

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

我正在使用这个 library用于管理 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'

最佳答案

确保将这些添加为 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"

同时确保确保google() 添加到您在项目级gradle 中的存储库 并且您已连接到internet !!

编辑

对于 androidx 使用:-

def lifecycle_version = "2.0.0"

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

关于java - 错误 : cannot access LifecycleObserver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49485847/

相关文章:

java - 如何使用 JOOQ 正确构建动态 WHERE 子句条件?

java - 监听 JTable 中 JCheckBox 的变化

java - 如何对 Double 而不是 Integer 进行 Java lambda 求和?

JavaFX ToggleButton 进入和退出无限循环

android - SQLite 中组的平均值之和

java - 从 apache 网站导入库

android - 有什么方法可以让我从我的 Windows PC 上运行 Android 操作系统和应用程序?

java - 在java中设置方法的运行时间限制

java - 使用 Tomcat 创建 Restful Client Consumer

android - 如何在 Webview 中调用 Barcode Scanner,并将其传回?