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

Posted

技术标签:

【中文标题】findViewById(R.id.videoView) 在片段中返回 Null (Android Studio)【英文标题】:findViewById(R.id.videoView) Returns Null in a Fragment (Android Studio) 【发布时间】:2021-11-23 20:53:33 【问题描述】:

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

我试图在片段中显示视频提要,但 findViewById(R.id.videoView) 总是抛出 NullPointException 错误。我相信我已经将问题缩小到 findViewById 返回 null 或 R.id.videoView 为 null(尽管已正确连接到 int)。

CameraView.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_
    android:layout_
    android:gravity="center">

    <VideoView
        android:id="@+id/videoView"
        android:layout_
        android:layout_
        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。任何帮助将不胜感激。

【问题讨论】:

new AppCompatActivity() 认真的吗? 你不应该自己创建活动实例你应该在onCreateView内部的view上使用findview并将结果存储在片段中的字段中 【参考方案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();
    




这两个都应该可以,选择你喜欢的,但我更喜欢绑定的。

【讨论】:

以上是关于findViewById(R.id.videoView) 在片段中返回 Null (Android Studio)的主要内容,如果未能解决你的问题,请参考以下文章

findViewById() 可能会产生 NullPointerException

FindViewById 循环

findviewbyid - 无法解决

xUtils,butterknife...处理findviewbyid

无法解析片段中的 findViewById [重复]

使用 findViewById() 找不到视图