简体   繁体   English

findViewById() 在片段中返回 null

[英]findViewById() returns null in fragment

So I think I have gone through all the answers here on stackoverflow for this problem.所以我想我已经在stackoverflow上解决了这个问题的所有答案。 However, noone of them have helped.然而,他们中没有人提供帮助。 I am currently working with the camera2 example by Google trying to add an imageview "imageOverlay" that covers the whole screen.我目前正在使用 Google 的 camera2 示例,尝试添加一个覆盖整个屏幕的图像视图“imageOverlay”。 However, imageview is always null when trying to retrieve it.但是,在尝试检索时,imageview 始终为 null。 Any idea what I am missing?知道我缺少什么吗?

Edit:编辑:

What has been tried:已经尝试过的:

  • Moving the ImageView in the camera_fragment.xml into the FrameLayout so that I can retrieve it just like the Button.将 camera_fragment.xml 中的 ImageView 移动到 FrameLayout 中,以便我可以像 Button 一样检索它。 This seemed like the best idea to make it an overlay.这似乎是使其成为叠加层的最佳主意。
  • Using getView()使用 getView()
  • Using getActivity()使用 getActivity()
  • Cleaning and rebuilding.清洁和重建。
  • Using View view = inflater.inflate(R.layout.camera_fragment, null);使用View view = inflater.inflate(R.layout.camera_fragment, null);

Camera_Activity xml: Camera_Activity xml:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/container"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#000"
   tools:context="com.irg.hig.imageregistrationgame.CameraActivity"
/>

camera_fragment.xml: camera_fragment.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.irg.hig.imageregistrationgame.CameraTextureView
        android:id="@+id/texture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <ImageView
        android:id="@+id/imageOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0.5" />

    <FrameLayout
        android:id="@+id/control"
        android:layout_width="match_parent"
        android:layout_height="112dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="@color/control_background">

        <Button
            android:id="@+id/picture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/picture" />

        <ImageButton
            android:id="@+id/info"
            android:contentDescription="@string/description_info"
            style="@android:style/Widget.Material.Light.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|right"
            android:padding="20dp" />

    </FrameLayout>
</RelativeLayout>

CameraFragment.java:相机片段.java:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.camera_fragment , container, false);
    imageView = (ImageView) view.findViewById(R.id.imageOverlay);

    return view;
}

    @Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    view.findViewById(R.id.picture).setOnClickListener(this);
    view.findViewById(R.id.info).setOnClickListener(this);
    mTextureView = (CameraTextureView) view.findViewById(R.id.texture);
    //imageView = (ImageView) view.findViewById(R.id.imageOverlay);
}

The problem was that there was a second xml file in layout-v21 that was used and not the one in the Layout folder that I was trying to change.问题是 layout-v21 中使用了第二个 xml 文件,而不是我试图更改的 Layout 文件夹中的那个。 Thanks to everyone that tried to help, it was appreciated immensely.感谢所有试图提供帮助的人,非常感谢。

Try this.尝试这个。

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.your_layout, null);

Try to replace : View view = inflater.inflate(R.layout.camera_fragment , container, false);尝试替换View view = inflater.inflate(R.layout.camera_fragment , container, false);

by View view = inflater.inflate(R.layout.camera_fragment, null);通过View view = inflater.inflate(R.layout.camera_fragment, null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM