代码之家  ›  专栏  ›  技术社区  ›  Niels Vanwingh

ImageSwitcher Android NullPointerException[重复]

  •  -1
  • Niels Vanwingh  · 技术社区  · 6 年前

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

    我的代码如下……

    我收到以下错误消息:

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

    这就意味着我没有实例化图像转换器,我相信我是…… 我错过了什么?

    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 回复  |  直到 6 年前
        1
  •  3
  •   Mahavir Jain    6 年前
     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)
                }
            })
    
        2
  •  0
  •   Niels Vanwingh    6 年前

    我找到了答案…… 忘记调用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;
        }
    }