Image slider inside fragment child using ViewFlipper
<p>I have tried image slider and solved error of .this file using getActivity().But as soon as i open the app it will stop how to solve that.</p>
<p>java code that extends fragment</p>
<h1>.java code</h1>
<pre><code>public class CargoHome extends Fragment {
View myView;
Animation fade_in, fade_out;
ViewFlipper viewFlipper;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.cargo_home, container, false);
    viewFlipper = (ViewFlipper)getActivity().findViewById(R.id.backvf);

    fade_in = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in);
    fade_out = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out);

    viewFlipper.setInAnimation(fade_in);
    viewFlipper.setOutAnimation(fade_out);
    viewFlipper.setAutoStart(true);
    viewFlipper.setFlipInterval(5000);
    viewFlipper.startFlipping();


    return myView;

}
</code></pre>
<p>}</p>
<p>xml code</p>
<h2>.xml code</h2>
<pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;match_parent&quot;&gt;

&lt;LinearLayout
    android:layout_width=&quot;fill_parent&quot;
    android:layout_height=&quot;fill_parent&quot;&gt;
    &lt;ViewFlipper
        android:id=&quot;@+id/backvf&quot;
        android:layout_width=&quot;fill_parent&quot;
        android:layout_height=&quot;fill_parent&quot;&gt;
        &lt;ImageView
            android:id=&quot;@+id/img1&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:scaleType=&quot;fitCenter&quot;
            android:src=&quot;@drawable/bolt&quot; /&gt;
        &lt;ImageView
            android:id=&quot;@+id/img2&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:scaleType=&quot;fitCenter&quot;
            android:src=&quot;@drawable/nano&quot; /&gt;
        &lt;ImageView
            android:id=&quot;@+id/img3&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:scaleType=&quot;fitCenter&quot;
            android:src=&quot;@drawable/zest&quot; /&gt;



    &lt;/ViewFlipper&gt;
 &lt;/LinearLayout&gt;
&lt;/RelativeLayout&gt;
</code></pre>
<p>fade_in and fade_out xml file</p>
<h3>fade_in and fade_out.xml</h3>
<pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;set  xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
    &lt;alpha
        android:duration=&quot;2000&quot;
        android:fromAlpha=&quot;0.0&quot;
        android:toAlpha=&quot;1.0&quot;
        android:interpolator=&quot;@android:anim/accelerate_interpolator&quot; /&gt;


&lt;/set&gt;
</code></pre>