如何在片段中实现自动图像滑块

How to Implement Auto Image Slider inside Fragment

我在我的项目中使用以下库来自动滑动图像。它在 Activity 中运行良好,但当涉及到 Fragment 时,当上下文传递为 getContext();getActivity(); 而不是 this; 时,它会抛出空指针异常。我现在坚持了下来。 请帮助。

图书馆名称:Link To Library

我的代码片段:

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();

我的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)

布局代码:

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

我的适配器没有问题,它适用于我拥有的另一个 activity。

没有看到 logcat 错误日志,很难理解这里的问题是什么。但是,我想我应该提出一些建议,以便您可以自己调查问题。

您可以考虑从 Fragment 初始化您的适配器,如下所示。

SliderImageAdapter adapter = new SliderImageAdapter(getActivity());

此外,请检查您使用的布局是否具有 ImageSlider 属性。如果您尝试扩充的片段布局中没有该布局 ID,则它也应该有一个空指针异常。

恕我直言,适配器看起来不错。

更新

从 logcat 看来,您用于 Fragment 的布局似乎没有 ImageSlider,因此无法初始化 sliderView .

从您发布的布局来看,布局 ID 似乎是 imageSlider,而不是 ImageSlider。布局 ID 区分大小写。

因此,您应该按如下方式修改以下行。

view.findViewById(R.id.imageSlider);