如何从扩展 Fragment 而不是 Activity 的 class 中正确检索声明到片段布局中的 ImageView?

How can I correctly retrieve an ImageView declared into a fragment layout from a class that extends Fragment and not Activity?

我在尝试将布局元素检索到 class 时遇到以下问题。

所以我有以下情况:

1) 我有这个 fragment_screen_slide_page.xml 文件,其中包含显示在视图中的片段元素:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Dummy content. -->
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="0dp">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:scaleType="fitCenter"
            android:layout_height="250dp"
            android:background="@drawable/carbonara" />

        <TextView android:id="@android:id/text1"
            style="?android:textAppearanceLarge"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp" />

    </LinearLayout>

</ScrollView>

如您所见,它包含具有 id=imageView1.

ImageView 元素

然后我有这个 ScreenSlidePageFragment 适用于前一个片段,我有这样的东西:

public class ScreenSlidePageFragment extends Fragment {

    ................................................................
    ................................................................
    ................................................................

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ................................................................
        ................................................................
        ................................................................
        // Retrieve the reference of the ImageView element of the layout having id=imgSlide:
        ImageView imgSlideView = (ImageView) this.getActivity().findViewById(R.id.imageView1);
        ................................................................
        ................................................................
        ................................................................
    }

    ................................................................
    ................................................................
    ................................................................
}

如您所见,在 onCreate() 方法中,我试图检索具有 [= 的元素的 ImageView 引用33=]id=imageView1定义到fragment_screen_slide_page.xml文件中,通过这条指令:

ImageView imgSlideView = (ImageView) this.getActivity().findViewById(R.id.imageView1);

我完成了 this.getActivity().findViewById(R.id.imageView1) 因为 ScreenSlidePageFragment 扩展了 Fragment 而不是 Activity 所以它没有继承 findViewById() 方法,我不能直接 findViewById(R.id.imageView1); 就像我对扩展 Activity.

所做的 class

问题是这样做我得到 null 作为结果。

为什么?我错过了什么?我怎样才能从 ScreenSlidePageFragment(扩展 Fragment 而不是 Activity)?

试试看: 将您的代码从 oncreate 移至覆盖的 onCreateView

public  View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
        View view = inflater.inflate(R.layout.fragment_screen_slide_page,container,false);
        ImageView imgSlideView = (ImageView) view.findViewById(R.id.imageView1);
        return view;    
}

onViewCreated

内调用
    @Override
    public void onViewCreated(View view, @Nullable Bundle  savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ImageView imgSlideView = (ImageView) view.findViewById(R.id.imageView1);
    }

而不是 onCreate(),您可以覆盖 onViewCreated() 并从那里获取您的 ImageView

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    ImageView imgSlideView = (ImageView) view.findViewById(R.id.imageView1);
}