findViewById()在片段中返回null

问题描述:

所以我想我已经通过了这个问题的所有答案在这里*这个问题。然而,他们中没有人帮助过。 我目前正在使用Google的camera2例子,试图添加一个覆盖整个屏幕的imageview“imageOverlay”。但是,尝试检索它时,imageview始终为空。任何想法我失踪?findViewById()在片段中返回null

编辑:

什么都试过:

  • 朝着camera_fragment.xml的ImageView的到的FrameLayout,这样我可以像按钮检索。这似乎是最好的想法,使其成为一个覆盖。
  • 使用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); 
} 
+0

如果你的CameraTextureView也是'match_parent'而不是'wrap_content'?这不会导致它为空,但它看起来像是一个单独的问题。 – AdamMc331

+0

当我将其更改为match_parent时,没有更改任何内容。作为一个方面说明,它是空的。 – Ikon

+0

您是否清理并重建了您的项目,以防万一?在Eclipse/ADT时代,资源不同步问题很常见,但在Android Studio上却不是完全没有。 – laalto

的问题是,有布局-V21第二个XML文件中使用的,而不是一个在我试图布局文件夹更改。 感谢大家的努力帮助,非常感谢。

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

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

+0

还是空即时通讯恐惧。 – Ikon

+0

你的“视图”呢?它也是空吗? – Nawako

+0

没有视图不为空。 – Ikon

试试这个。

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