AndreaNobili
AndreaNobili

Reputation: 42957

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

I have the following problem trying to retrieve a layout element into a class.

So I have the following situation:

1) I have this fragment_screen_slide_page.xml file that contains the element of a fragment that is shown into a view:

<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>

As you can see it contains the ImageView element having id=imageView1.

Then I have this ScreenSlidePageFragment that works on the previous fragment, I have something like this:

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);
        ................................................................
        ................................................................
        ................................................................
    }

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

As you can see, into the onCreate() method I am trying to retrieve the ImageView refernce of the element having id=imageView1 defined into the fragment_screen_slide_page.xml file, by this instruction:

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

I have done this.getActivity().findViewById(R.id.imageView1) because the ScreenSlidePageFragment extends Fragment and not Activity so it have not inherited the findViewById() method and I can't do diretly findViewById(R.id.imageView1); as I done into a class that extends Activity.

The problem is that doing in this way I obtain null as result.

Why? What am I missing? How can I correctly retrieve my ImageView declared into the fragment_screen_slide_page.xml file from the ScreenSlidePageFragment (that extends Fragment and not Activity)?

Upvotes: 0

Views: 159

Answers (3)

Asmaa Rashad
Asmaa Rashad

Reputation: 603

Try That: move your code from oncreate to the overriden 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;    
}

Upvotes: 1

earthw0rmjim
earthw0rmjim

Reputation: 19417

Instead of onCreate(), you could override onViewCreated() and get your ImageView from there:

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

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

Upvotes: 1

Enzokie
Enzokie

Reputation: 7415

Call it within onViewCreated

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

Upvotes: 1

Related Questions