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'
答案 0 :(得分:2)
确保将这些添加为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中的repositories
,并且您已连接到互联网 !!
修改强>
对于androidx使用: -
def lifecycle_version = "2.0.0"
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
答案 1 :(得分:1)
在 gradle.properties
中添加这两行android.useAndroidX=true
android.enableJetifier=true
当添加这两行时,您将在活动中得到一个错误。 您需要将所有组件从 android 修改为 androidX
例如: android.support.v7.app.AppCompatActivity
至androidx.appcompat.app.AppCompatActivity
答案 2 :(得分:-1)
您应该从LifecycleActivity扩展而不是扩展普通的Activity。
否则,您可以参考"在自定义活动和片段中实施LifecycleOwner":