2015-11-04 64 views
-1

所以我想我已經通過了這個問題的所有答案在這裏stackoverflow這個問題。然而,他們中沒有人幫助過。 我目前正在使用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

回答

0

的問題是,有佈局-V21第二個XML文件中使用的,而不是一個在我試圖佈局文件夾更改。 感謝大家的努力幫助,非常感謝。

-1

嘗試取代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

0

試試這個。

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