Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从扩展片段而不是活动的类中正确检索声明到片段布局中的ImageView?_Java_Android_Android Layout_Android Fragments_Android Imageview - Fatal编程技术网

Java 如何从扩展片段而不是活动的类中正确检索声明到片段布局中的ImageView?

Java 如何从扩展片段而不是活动的类中正确检索声明到片段布局中的ImageView?,java,android,android-layout,android-fragments,android-imageview,Java,Android,Android Layout,Android Fragments,Android Imageview,我在尝试将布局元素检索到类中时遇到以下问题 因此,我有以下情况: 1) 我有一个fragment\u screen\u slide\u page.xml文件,其中包含显示在视图中的片段元素: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content" android:layout_width="match_parent" androi

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

因此,我有以下情况:

1) 我有一个fragment\u screen\u slide\u 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>
如您所见,在onCreate()方法中,我试图检索元素的ImageView引用,该元素的id=imageView1定义在fragment\u screen\u slide\u page.xml文件中,通过以下指令:

ImageView imgSlideView = (ImageView) this.getActivity().findViewById(R.id.imageView1);
我之所以这样做.getActivity().findViewById(R.id.imageView1),是因为ScreenSlidePageFragment扩展了片段,而不是活动,因此它没有继承findViewById()方法,我无法直接findViewById(R.id.imageView1)就像我在扩展活动的类中所做的那样

问题是这样做的结果是null

为什么??我错过了什么?如何从ScreenSlidePageFragment(扩展fragment而不是Activity)中正确检索声明到fragment\u screen\u slide\u page.xml文件中的图像视图?

尝试: 将代码从
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;    
}
试试看: 将代码从
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);
    }

onViewCreated

    @Override
    public void onViewCreated(View view, @Nullable Bundle  savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ImageView imgSlideView = (ImageView) view.findViewById(R.id.imageView1);
    }
您可以覆盖
onViewCreated()
,而不是
onCreate()
,从那里获取您的
ImageView

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

    ImageView imgSlideView = (ImageView) view.findViewById(R.id.imageView1);
}
您可以覆盖
onViewCreated()
,而不是
onCreate()
,从那里获取您的
ImageView

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

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

为什么不试试文档?为什么不试试文档?