findViewById(R.id.videoView) 在片段中返回 null (Android Studio)

findViewById(R.id.videoView) Returns Null in a Fragment (Android Studio)

提问人:Aperture Lab Gamer 提问时间:10/3/2021 更新时间:10/3/2021 访问量:436

问:

首先,我已经查看了与此问题相关的所有其他帖子,但没有任何问题解决。

我尝试在 Fragment 中显示视频源,但 findViewById(R.id.videoView) 总是抛出 NullPointException 错误。我相信我已经将问题缩小到findViewById返回null或以某种方式R.id.videoView为null(尽管已正确连接到int)。

相机视图.class:

public class CameraView extends Fragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.cameraview_layout, container, false);
        playVideo();
        return view;
    }

    public void playVideo(){
        VideoView myVideoView = (VideoView) new AppCompatActivity().findViewById(R.id.videoView);
        myVideoView.setVideoURI(Uri.fromFile(new File("C:\\Users\\USER\\Videos\\video.mp4")));
        myVideoView.setMediaController(new MediaController(myVideoView.getContext()));
        myVideoView.requestFocus();
        myVideoView.start();
    }


}

cameraview_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="342dp"
        android:layout_height="288dp"
        android:layout_marginTop="149dp"
        android:layout_marginBottom="208dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

我不确定为什么 findViewById 仍然返回 null,即使在尝试关注这里和其他地方的许多不同帖子之后也是如此。据我所知,videoView是一个有效的ID。任何帮助将不胜感激。

android android-studio android-fragments 视频 nullpointerexception

评论

1赞 Selvin 10/3/2021
new AppCompatActivity()认真地?您永远不应该自己创建活动实例,您应该在内部使用 FindView,并将结果存储在 Fragment 的字段中viewonCreateView

答:

0赞 Omar Shawky 10/3/2021 #1

您应该将对视图或您在 onCreateView 中创建的绑定的引用保留为片段中的全局变量,并使用它来获取视图对象。

public class CameraView extends Fragment {
    View rootView ;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.cameraview_layout, container, false);
        playVideo();
        return rootView;
    }

    public void playVideo(){
        VideoView myVideoView = (VideoView)rootView.findViewById(R.id.videoView);
        myVideoView.setVideoURI(Uri.fromFile(new File("C:\\Users\\USER\\Videos\\video.mp4")));
        myVideoView.setMediaController(new MediaController(myVideoView.getContext()));
        myVideoView.requestFocus();
        myVideoView.start();
    }


}

public class CameraView extends Fragment {
    CameraviewLayoutBinding binding;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = CameraviewLayoutBinding.inflate(inflater, container, false);
        playVideo();
        return binding.getRoot();
    }

    public void playVideo(){
        VideoView myVideoView = binding.videoView;
        myVideoView.setVideoURI(Uri.fromFile(new File("C:\\Users\\USER\\Videos\\video.mp4")));
        myVideoView.setMediaController(new MediaController(myVideoView.getContext()));
        myVideoView.requestFocus();
        myVideoView.start();
    }


}

这两个都应该有效,选择你喜欢的,但我更喜欢有约束力的。