【问题标题】:findViewById() returns null in fragmentfindViewById() 在片段中返回 null
【发布时间】:2015-11-04 20:14:55
【问题描述】:

所以我想我已经在 stackoverflow 上解决了这个问题的所有答案。然而,没有人提供帮助。 我目前正在使用 Google 的 camera2 示例,试图添加一个覆盖整个屏幕的图像视图“imageOverlay”。但是,在尝试检索时,imageview 始终为 null。知道我缺少什么吗?

编辑:

什么都试过了:

  • 将 camera_fragment.xml 中的 ImageView 移动到 FrameLayout 中,这样我就可以像 Button 一样检索它。这似乎是使其成为叠加层的最佳主意。
  • 使用 getView()
  • 使用 getActivity()
  • 清理和重建。
  • 使用View view = inflater.inflate(R.layout.camera_fragment, null);

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:

    <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:

    @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);
}

【问题讨论】:

  • 您的 CameraTextureView 是否也应该是 match_parent 而不是 wrap_content?这不会导致它为空,但它看起来确实是一个单独的问题。
  • 当我将其更改为 match_parent 时没有更改任何内容。作为旁注,它是空的。
  • 您是否清理并重建了您的项目,以防万一?资源不同步问题在 Eclipse/ADT 时代很常见,但在 Android Studio 中并非完全不存在。
  • 是的,3 次。现在4,以防万一。是我读到的答案之一。
  • 尝试将 imageOverlay 重命名为其他名称,然后尝试

标签: java android xml android-layout android-studio


【解决方案1】:

问题是在 layout-v21 中使用了第二个 xml 文件,而不是我试图更改的 Layout 文件夹中的那个。 感谢所有试图提供帮助的人,非常感谢。

【讨论】:

    【解决方案2】:

    试试这个。

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

    【讨论】:

      【解决方案3】:

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

      作者 View view = inflater.inflate(R.layout.camera_fragment, null);

      【讨论】:

      • 恐怕还是无效。
      • 你的view 呢?也是空的吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      • 2021-11-23
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 2016-02-16
      • 1970-01-01
      相关资源
      最近更新 更多