I do in onCreate :

 viewPager =(ViewPager)getActivity().findViewById(R.id.main_content); 

This in onCreateView :

 viewPager.setBackgroundResource(R.drawable.potehki_fon); 

Markup:

 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/main_content" android:fitsSystemWindows="true" > <android.support.v4.view.ViewPager android:id="@+id/pager" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" /> <ImageView android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end|bottom" android:layout_margin="10dp" android:src="@drawable/speaker_icon_on_shadow" /> </android.support.design.widget.CoordinatorLayout> 

When executing the code I see the following error:

  java.lang.ClassCastException: android.support.design.widget.CoordinatorLayout cannot be cast to android.support.v4.view.ViewPager at com.example.potehki.potehki.PageFragment.onCreate(PageFragment.java:63) 

Closed due to the fact that the participants are off topic : , zRrr , aleksandr barakin , Nick Volynkin 11 May '16 at 6:55 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Yuriy SPb, zRrr, aleksandr barakin, Nick Volynkin
If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    The compiler tells you that you mixed one twist with another. Namely, you are trying to find the ViewPager by the main_content ID, whereas with this ID you have CoordinatorLayout.

    Read the compiler messages and be careful.

    • sorry, bad copied) CoordinatorLayout coordinatorLayout; coordinatorLayout = (CoordinatorLayout) getActivity (). findViewById (R.id.main_content); coordinatorLayout.setBackgroundResource (R.drawable.potehki_fon); so you can do it or is it gavn.kod? - upward
    • @upward, if you want to change the background when scrolling through fragments, then it should change the background, but not quite as you would expect. In this case, it is better to hang the listener turning the pages on the pager itself and change the background in it depending on the position of the selected fragment. - Yuriy SPb
    • I did just that, in this thread: the link only slows me through scrolling, deletes the background and doesn't lag anything, so I'm looking for a way out - upward
    • one
      @upward, well, then the problem is that the background file is too large, but not in the way it is assigned. Reduce the background file size. - Yuriy SPb
    • I can understand something, affairs. like this: coordinatorLayout.setBackgroundResource (R.drawable.fon1); I check it in View onCreateView then: if (pageNumber == 1) I do this: coordinatorLayout.setBackgroundResource (R.drawable.fon2); But actually: the application is launched, but the background does not change, that is, when I scroll through, I see fon1 for fragment 2, but it should be fon2 - upward