gpt4 book ai didi

java - 上下文在Fragment中返回null

转载 作者:行者123 更新时间:2023-12-01 16:50:45 26 4
gpt4 key购买 nike

我一直很努力地在Fragment中获取空上下文。我已经阅读了所有与我相同的问题,但没有解决任何问题。我尝试了onAttach等等,但到目前为止没有任何效果。
我在这里遇到错误:


播放器= ExoPlayerFactory.newSimpleInstance(getActivity());


其中getActivity()返回null

我将片段粘贴在这里,以便任何人可以看看并建议我该怎么做

public class Fragment1 extends Fragment {
private Frag1LayoutBinding binding;
private PlayerView playerView;
private SimpleExoPlayer player;
private Button button;

private boolean playWhenReady = true;
private int currentWindow = 0;
private long playbackPosition = 0;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = Frag1LayoutBinding.inflate(inflater, container, false);
binding.button2.setText("Live");
button = binding.button2.findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initializePlayer();
}
});
return binding.getRoot();
}

private void initializePlayer() {
player = ExoPlayerFactory.newSimpleInstance(getActivity());
playerView.setPlayer(player);
Uri uri = Uri.parse("https://www.radiantmediaplayer.com/media/bbb-360p.mp4");
MediaSource mediaSource = buildMediaSource(uri);
}

private MediaSource buildMediaSource(Uri uri) {
DataSource.Factory dataSourceFactory =
new DefaultDataSourceFactory(getActivity(), "exoplayer-codelab");
return new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
}

@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}

@Override
public void onStart() {
super.onStart();
if(Util.SDK_INT >= 24) {
initializePlayer();
}
}

@Override
public void onResume() {
super.onResume();
hideSystemUi();
if((Util.SDK_INT < 24 || player == null)) {
initializePlayer();
}
}

@Override
public void onPause() {
super.onPause();
if (Util.SDK_INT < 24) {
releasePlayer();
}
}

@Override
public void onStop() {
super.onStop();
if (Util.SDK_INT >= 24) {
releasePlayer();
}
}

private void releasePlayer() {
if (player != null) {
playWhenReady = player.getPlayWhenReady();
playbackPosition = player.getCurrentPosition();
currentWindow = player.getCurrentWindowIndex();
player.release();
player = null;
}
}

@SuppressLint("InlinedApi")
private void hideSystemUi() {
playerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}


我得到的错误是


java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void com.google.android.exoplayer2.ui.PlayerView.setPlayer(com.google.android.exoplayer2.Player)'
在com.example.tv_1.Fragment1.initializePlayer(Fragment1.java:58)
在com.example.tv_1.Fragment1.onStart(Fragment1.java:79)

最佳答案

您忘了找到playerView,这意味着方法initializePlayer中将崩溃

playerView.setPlayer(player);

关于java - 上下文在Fragment中返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61695953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com