所以我想我已經通過了這個問題的所有答案在這裏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);
}
如果你的CameraTextureView也是'match_parent'而不是'wrap_content'?這不會導致它爲空,但它看起來像是一個單獨的問題。 – AdamMc331
當我將其更改爲match_parent時,沒有更改任何內容。作爲一個方面說明,它是空的。 – Ikon
您是否清理並重建了您的項目,以防萬一?在Eclipse/ADT時代,資源不同步問題很常見,但在Android Studio上卻不是完全沒有。 – laalto