ImageSwitcher Android NullPointerException

时间:2018-06-25 10:16:30

标签: android

我正在使用ImageSwitcher视图在3张图片之间创建无限循环。每秒在视图中显示另一张图像。

我的代码如下。

我收到以下错误消息:

“ java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.ImageView.setImageResource(int)'”

这表示我没有实例化ImageSwitcher,但我相信我是。 我想念什么?

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

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

我找到了答案。 忘记调用setFactory()方法。 什么对我有用。

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