[英]findViewById() returns null in fragment
所以我想我已經在stackoverflow上解決了這個問題的所有答案。 然而,他們中沒有人提供幫助。 我目前正在使用 Google 的 camera2 示例,嘗試添加一個覆蓋整個屏幕的圖像視圖“imageOverlay”。 但是,在嘗試檢索時,imageview 始終為 null。 知道我缺少什么嗎?
編輯:
已經嘗試過的:
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>
相機片段.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);
}
問題是 layout-v21 中使用了第二個 xml 文件,而不是我試圖更改的 Layout 文件夾中的那個。 感謝所有試圖提供幫助的人,非常感謝。
嘗試這個。
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.your_layout, null);
嘗試替換: View view = inflater.inflate(R.layout.camera_fragment , container, false);
通過View view = inflater.inflate(R.layout.camera_fragment, null);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.