-1

I am using an ImageSwitcher View to create an infinite loop between 3 pictures.. Every second another image is being displayed in the View.

My code is the following..

I am getting the following error message:

"java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference"

This would say I am not instantiating the ImageSwitcher, which I believe I am.. What am I missing?

public class SecAct_Foto_Fragment extends Fragment {

    int counter = 0;
    View rootView;
    private ImageSwitcher pic_image_switch;
    private Handler pic_image_switch_handler;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.sec_act_photo_layout, container, false);

        pic_image_switch = (ImageSwitcher) rootView.findViewById(R.id.foto_groot_imageswitch);

        pic_image_switch_handler = new Handler(Looper.getMainLooper());

        pic_image_switch_handler.post(new Runnable() {
            @Override
            public void run() {
                switch (counter) {
                    case 0:
                        pic_image_switch.setImageResource(R.drawable.run_mount);
                        break;
                    case 1:
                        pic_image_switch.setImageResource(R.drawable.run_away);
                        break;
                    case 2:
                        pic_image_switch.setImageResource(R.drawable.run_rocks);
                        break;
                }
                counter += 1;
                if (counter == 3) {
                    counter = 0;
                }
                pic_image_switch.postDelayed(this, 1000);
            }
        });

        return rootView;
    }
}

Fragment XML :

     <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@color/white"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

     <ImageSwitcher
        android:id="@+id/foto_groot_imageswitch"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:background="@color/black"
        android:contentDescription="@string/topscreen_picture_secondactivity"
        android:padding="3dp"
        android:scaleType="fitXY"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/run_rocks"
        />

    <ImageView
        android:id="@+id/knoppen"
        android:layout_width="120dp"
        android:layout_height="25dp"
        android:layout_marginBottom="264dp"
        android:contentDescription="@string/threebuttons_secondact"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:srcCompat="@drawable/but_left" />

</android.support.constraint.ConstraintLayout>
Niels Vanwingh
  • 604
  • 1
  • 6
  • 22

2 Answers2

2
 val pic_image_switch = findViewById(R.id.imageswitch) as ImageSwitcher
        pic_image_switch.setFactory(object : ViewSwitcher.ViewFactory {
            override fun makeView(): View {
                val imageview = ImageView(this@MainActivity)
                return imageview;
            }
        })


        pic_image_switch_handler = Handler();
        pic_image_switch_handler!!.post(object : Runnable {
            override fun run() {
                when (counter) {
                    0 -> mBinding.imageswitch.setImageResource(R.drawable.ic_pic_one)
                    1 -> mBinding.imageswitch.setImageResource(R.drawable.ic_pic_two)
                    2 -> mBinding.imageswitch.setImageResource(R.drawable.ic_pic_three)
                }
                counter += 1
                if (counter === 3) {
                    counter = 0
                }
                pic_image_switch_handler!!.postDelayed(this, 1000)
            }
        })
Mahavir Jain
  • 1,448
  • 10
  • 23
0

I found the answer.. Forgot to call the setFactory() method.. What is working for me..

public class SecAct_Foto_Fragment extends Fragment {

    int counter = 0;
    View rootView;
    private ImageSwitcher pic_image_switch;
    private Handler pic_image_switch_handler;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.sec_act_photo_layout, container, false);

        /*Animation anim_in = AnimationUtils.loadAnimation(getActivity(), R.anim.enter_from_left);
        pic_image_switch.setInAnimation(anim_in);*/

        //pic_image_switch = new ImageSwitcher(getActivity());
        pic_image_switch = (ImageSwitcher) rootView.findViewById(R.id.foto_groot_imageswitch);

        pic_image_switch.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(getActivity());
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
                return imageView;
            }
        });

        pic_image_switch_handler = new Handler(Looper.getMainLooper());

        pic_image_switch_handler.post(new Runnable() {
            @Override
            public void run() {
                switch (counter) {
                    case 0:
                        pic_image_switch.setImageResource(R.drawable.run_mount);
                        break;
                    case 1:
                        pic_image_switch.setImageResource(R.drawable.run_away);
                        break;
                    case 2:
                        pic_image_switch.setImageResource(R.drawable.run_rocks);
                        break;
                }
                counter += 1;
                if (counter == 3) {
                    counter = 0;
                }
                pic_image_switch.postDelayed(this, 1000);
            }
        });

        return rootView;
    }
}
Niels Vanwingh
  • 604
  • 1
  • 6
  • 22