findViewById()在片段中返回null

时间:2015-11-04 20:14:55

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

所以我想我已经在stackoverflow上解决了这个问题的所有答案。然而,他们中没有人帮助过。 我目前正在使用Google的示例,Google试图添加一个imageview" 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);
}

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

试试这个。

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

答案 2 :(得分:-1)

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

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

相关问题