ImageSwitcher Android NullPointerException

Niels Vanwingha

ImageSwitcherビューを使用して、3つの画像の間に無限ループを作成しています。1秒ごとに別の画像がビューに表示されています。

私のコードは次のとおりです。

次のエラーメッセージが表示されます。

"java.lang.NullPointerException:nullオブジェクト参照で仮想メソッド '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>
マハーヴィーラジャイナ教
 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)
            }
        })

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Android ImageSwitcher - 画像を切り替えるときの OutOfMemory (非常に小さい)

分類Dev

Android MediaRecorder NullPointerException

分類Dev

Android BottomSheetBehavior setState()NullPointerException

分類Dev

android MediaPlayer NullPointerException

分類Dev

Android ArrayAdapter NullPointerException getID

分類Dev

Android NullPointerException + GetDatabaseLocked

分類Dev

Android:findViewById NullpointerException

分類Dev

Android NullPointerException Error Message

分類Dev

Android onClickListener NullPointerException

分類Dev

nullpointerexception on launcher? android

分類Dev

android bindService()NullPointerException

分類Dev

android bindService()NullPointerException

分類Dev

NullPointerException onCreate()Android Studio

分類Dev

Android AIDL iBinder NullPointerException

分類Dev

NullPointerException Android getContext()

分類Dev

Android-RecyclerViewのNullPointerException

分類Dev

NullPointerException with Sharing Intent in Android Nougat

分類Dev

Android-NullPointerException:storage == null

分類Dev

nullPointerExceptionヘルプ(android)

分類Dev

Androidの不明なNullPointerException

分類Dev

android webview CustomView onHideCustomView nullpointerexception

分類Dev

android webview CustomView onHideCustomView nullpointerexception

分類Dev

AndroidのDsClientManagerのNullPointerException

分類Dev

Android-RecyclerView NullPointerException getItemCount?

分類Dev

JUnitテストAndroid NullPointerException

分類Dev

JUnitテストAndroid NullPointerException

分類Dev

Google Maps API Android-NullPointerException-setBoundsInParent

分類Dev

Google Maps API Android-NullPointerException-setBoundsInParent

分類Dev

Android.os.BundleのNullPointerException

Related 関連記事

  1. 1

    Android ImageSwitcher - 画像を切り替えるときの OutOfMemory (非常に小さい)

  2. 2

    Android MediaRecorder NullPointerException

  3. 3

    Android BottomSheetBehavior setState()NullPointerException

  4. 4

    android MediaPlayer NullPointerException

  5. 5

    Android ArrayAdapter NullPointerException getID

  6. 6

    Android NullPointerException + GetDatabaseLocked

  7. 7

    Android:findViewById NullpointerException

  8. 8

    Android NullPointerException Error Message

  9. 9

    Android onClickListener NullPointerException

  10. 10

    nullpointerexception on launcher? android

  11. 11

    android bindService()NullPointerException

  12. 12

    android bindService()NullPointerException

  13. 13

    NullPointerException onCreate()Android Studio

  14. 14

    Android AIDL iBinder NullPointerException

  15. 15

    NullPointerException Android getContext()

  16. 16

    Android-RecyclerViewのNullPointerException

  17. 17

    NullPointerException with Sharing Intent in Android Nougat

  18. 18

    Android-NullPointerException:storage == null

  19. 19

    nullPointerExceptionヘルプ(android)

  20. 20

    Androidの不明なNullPointerException

  21. 21

    android webview CustomView onHideCustomView nullpointerexception

  22. 22

    android webview CustomView onHideCustomView nullpointerexception

  23. 23

    AndroidのDsClientManagerのNullPointerException

  24. 24

    Android-RecyclerView NullPointerException getItemCount?

  25. 25

    JUnitテストAndroid NullPointerException

  26. 26

    JUnitテストAndroid NullPointerException

  27. 27

    Google Maps API Android-NullPointerException-setBoundsInParent

  28. 28

    Google Maps API Android-NullPointerException-setBoundsInParent

  29. 29

    Android.os.BundleのNullPointerException

ホットタグ

アーカイブ