How to Implement Auto Image Slider inside Fragment

CodeRED Innovations :

I'm Using the Following Library in my project to Auto Slide images. It works fine inside Activities but when it comes to Fragment it throws a null pointer exception when the context is passed As getContext();or getActivity(); instead of this;. I stuck with that now. Please Help.

Library name: Link To Library

My Code Inside Fragment:

SliderView sliderView = view.findViewById(R.id.ImageSlider);

SliderImageAdapter adapter = new SliderImageAdapter(getContext());
adapter.setCount(5);

sliderView.setSliderAdapter(adapter);

sliderView.setIndicatorAnimation(IndicatorAnimations.SLIDE); //set indicator animation by using SliderLayout.IndicatorAnimations. :WORM or THIN_WORM or COLOR or DROP or FILL or NONE or SCALE or SCALE_DOWN or SLIDE and SWAP!!
sliderView.setSliderTransformAnimation(SliderAnimations.CUBEINROTATIONTRANSFORMATION);
sliderView.setAutoCycleDirection(SliderView.AUTO_CYCLE_DIRECTION_BACK_AND_FORTH);
sliderView.setIndicatorSelectedColor(Color.WHITE);
sliderView.setIndicatorUnselectedColor(Color.GRAY);
sliderView.startAutoCycle();

My Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.coderedinnovations.allioservices, PID: 7955
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.smarteist.autoimageslider.SliderView.setSliderAdapter(com.smarteist.autoimageslider.SliderViewAdapter)' on a null object reference
    at com.coderedinnovations.allioservices.AllioMain.HomeFragment.onCreateView(HomeFragment.java:53)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
    at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2629)
    at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
    at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2722)
    at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:346)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1188)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
    at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625)
    at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
    at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247)
    at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)
    at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1433)
    at android.app.Activity.performStart(Activity.java:7987)
    at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3521)
    at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:226)
    at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:206)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:178)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:102)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2222)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:228)
    at android.app.ActivityThread.main(ActivityThread.java:7772)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981)

Layout Code:

<androidx.cardview.widget.CardView
    app:cardCornerRadius="10dp"
    android:layout_margin="14dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.smarteist.autoimageslider.SliderView
        android:id="@+id/imageSlider"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:sliderAnimationDuration="600"
        app:sliderAutoCycleDirection="back_and_forth"
        app:sliderAutoCycleEnabled="true"
        app:sliderIndicatorAnimationDuration="600"
        app:sliderIndicatorGravity="center_horizontal|bottom"
        app:sliderIndicatorMargin="15dp"
        app:sliderIndicatorOrientation="horizontal"
        app:sliderIndicatorPadding="3dp"
        app:sliderIndicatorRadius="0.5dp"
        app:sliderIndicatorSelectedColor="#5A5A5A"
        app:sliderIndicatorUnselectedColor="#FFF"
        app:sliderScrollTimeInSec="1"
        app:sliderStartAutoCycle="true" />

</androidx.cardview.widget.CardView>

There is no problem with my adapter and its working for the other activity that I had.

Reaz Murshed :

Without seeing the logcat error logs, it is difficult to understand what is the problem here. However, I thought I should put some suggestions so that you could investigate the problem yourself.

You might consider initializing your adapter from the Fragment as follows.

SliderImageAdapter adapter = new SliderImageAdapter(getActivity());

Also, please check if the layout that you are using has the ImageSlider attribute. If you do not have that layout id in your fragment's layout that you are trying to inflate, it should have a null pointer exception as well.

The adapter is looking fine IMHO.

Update

From the logcat, it looks like the layout that you are using for your Fragment does not have the ImageSlider and hence it could not initialize the sliderView.

From the layout that you posted, seems like the layout id is imageSlider, not ImageSlider. The layout ids are case-sensitive.

Hence you should have the following line modified as follows.

view.findViewById(R.id.imageSlider);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=25293&siteId=1