如何在 Butterknife onclick 函数中访问 YouTubePlayerView
How to access YouTubePlayerView inside Butterknife onlick function
使用此方法会引发空对象引用。 Butterknife 不允许在 void go(){
函数内部查看任何引用。有没有办法在函数内部访问此视图 YouTubePlayerView youTubePlayerView = player_view;
?
.java
@BindView(R.id.youtube_player_view) YouTubePlayerView player_view;
YouTubePlayerView youTubePlayerView = player_view;
//start view trailer
@OnClick(R.id.iv_play_circle)
void go(){
String vid = video.getVideoKey();
if(vid != null){
youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
@Override
public void onReady(@NonNull YouTubePlayer youTubePlayer) {
youTubePlayer.loadVideo(vid, 0);
}
});
}
}//end view trailer
@Override
public void onDestroyView () {
if(youTubePlayerView != null) {
youTubePlayerView.release();
}
super.onDestroyView ();
}
错误日志
E/CustomActivityOnCrash: The previous app process crashed. This is the stack trace of the crash:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView.addYouTubePlayerListener(com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.YouTubePlayerListener)' on a null object reference
@BindView(R.id.youtube_player_view) YouTubePlayerView player_view;
YouTubePlayerView youTubePlayerView = player_view;
此处您期望 youTubePlayerView
的值与 player_view
视图变量的 'filled' 值相同,但实际上,此分配是在player_view
绑定到视图之前的时间。
那是因为@BindView
是在你的onCreate()
上调用了ButterKnife.bind(this)
之后生效的,而你的赋值是在class初始化时完成的。
要么直接使用 player_view
,要么在调用 ButterKnife.bind(this)
之后执行 youTubePlayerView = player_view;
。
使用此方法会引发空对象引用。 Butterknife 不允许在 void go(){
函数内部查看任何引用。有没有办法在函数内部访问此视图 YouTubePlayerView youTubePlayerView = player_view;
?
.java
@BindView(R.id.youtube_player_view) YouTubePlayerView player_view;
YouTubePlayerView youTubePlayerView = player_view;
//start view trailer
@OnClick(R.id.iv_play_circle)
void go(){
String vid = video.getVideoKey();
if(vid != null){
youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
@Override
public void onReady(@NonNull YouTubePlayer youTubePlayer) {
youTubePlayer.loadVideo(vid, 0);
}
});
}
}//end view trailer
@Override
public void onDestroyView () {
if(youTubePlayerView != null) {
youTubePlayerView.release();
}
super.onDestroyView ();
}
错误日志
E/CustomActivityOnCrash: The previous app process crashed. This is the stack trace of the crash:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView.addYouTubePlayerListener(com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.YouTubePlayerListener)' on a null object reference
@BindView(R.id.youtube_player_view) YouTubePlayerView player_view;
YouTubePlayerView youTubePlayerView = player_view;
此处您期望 youTubePlayerView
的值与 player_view
视图变量的 'filled' 值相同,但实际上,此分配是在player_view
绑定到视图之前的时间。
那是因为@BindView
是在你的onCreate()
上调用了ButterKnife.bind(this)
之后生效的,而你的赋值是在class初始化时完成的。
要么直接使用 player_view
,要么在调用 ButterKnife.bind(this)
之后执行 youTubePlayerView = player_view;
。