Home > Blockchain >  fragment_main layout resource not getting created in runtime. getting NullPointerException even in o
fragment_main layout resource not getting created in runtime. getting NullPointerException even in o

Time:12-19

I'm a beginner level developer learning to develop Android apps using Kotlin and I've run into some problem which I'm not able to come out of it on my own.

Here's the problem:

enter image description here

In the above image, there plenty of other UI objects in the layout just like startBtn which is a FloatingActionButton without a ? beside its type declaration. Those objects are working fine in runtime and I've no problem dealing with those objects.

But, The once with ? such as suryaKriyaCard which is of type MaterialCardView?. these view objects are not getting bound and they raise NullPointerException

I've seen a few threads here on stackoverflow which says that I'm trying to access those objects a bit earlier than I should and I need to move that code to onViewCreated() method since it'll be called by the system once all the view objects are inflated on the fragment.

Here's the code implementation:

  override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    mainFragmentBinding = FragmentMainBinding.inflate(inflater,container,false)
    return mainFragmentBinding.root
}


  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    if (mContext == null) {
        mContext = mainFragmentBinding.root.context
    }
    activity = requireActivity()

    manager = ReviewManagerFactory.create(mContext)
    getReviewInfo()

    utils = Utilities()

    builder = AlertDialog.Builder(mContext)

    mainFragmentBinding.startBtn.setOnClickListener(this)

I'm always getting a NullPointerException at :

suryaKriyaUIcard = mainFragmentBinding.suryaKriyaCard!!
    suryaKriyaAd = mainFragmentBinding.suryaKriyaCardBelowAd!!
    shambhaviResumeBtn = mainFragmentBinding.shambhaviResumeBtn!!

Here's the layout xml code specific to crash:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.core.widget.NestedScrollView
        android:id="@ id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:filterTouchesWhenObscured="true">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white">

            <com.google.android.material.card.MaterialCardView
                android:id="@ id/invocation_card"
                style="@style/kriya_card_dimensions"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@ id/invocation_child"
                    style="@style/kriya_card_styles">

                    <com.google.android.material.textview.MaterialTextView
                        android:id="@ id/invocation_banner"
                        style="@style/kriya_card_header_styles"
                        android:contentDescription="@string/invocation_card_header"
                        android:text="@string/invocation_card_header"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <ProgressBar
                        android:id="@ id/play_in_progress_indicator"
                        style="@style/progress_bar_indicator_styles"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent" />


                    <com.google.android.material.floatingactionbutton.FloatingActionButton
                        android:id="@ id/start_btn"
                        style="@style/floating_play_buttons"
                        android:contentDescription="@string/play_invocation"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/invocation_banner" />

                    <com.google.android.material.textview.MaterialTextView
                        android:id="@ id/play_duration"
                        style="@style/kriya_sequence_time_stamps_and_rep_styles"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@ id/invocation_banner" />

                </androidx.constraintlayout.widget.ConstraintLayout>
            </com.google.android.material.card.MaterialCardView>

            <com.google.android.gms.ads.AdView
                android:id="@ id/invocation_card_below_ad"
                style="@style/banner_ad_styles"
                ads:adSize="BANNER"
                ads:adUnitId="@string/invocation_card_below_ad_unit_id"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/invocation_card" />

            <com.google.android.material.card.MaterialCardView
                android:id="@ id/crash_course_card"
                style="@style/kriya_card_dimensions"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/invocation_card_below_ad">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@ id/crash_course_card_child"
                    style="@style/kriya_card_styles">

                    <com.google.android.material.textview.MaterialTextView
                        android:id="@ id/crash_course_banner"
                        style="@style/kriya_card_header_styles"
                        android:contentDescription="@string/crash_course_header"
                        android:text="@string/inner_engineering_crash_course_card_header"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <ProgressBar
                        android:id="@ id/crash_course_in_progress_indicator"
                        style="@style/progress_bar_indicator_styles"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent" />


                    <com.google.android.material.floatingactionbutton.FloatingActionButton
                        android:id="@ id/crash_course_start_btn"
                        style="@style/floating_play_buttons"
                        android:contentDescription="@string/play_sadhguru_crash_course"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/crash_course_banner" />

                    <com.google.android.material.textview.MaterialTextView
                        android:id="@ id/crash_course_duration"
                        style="@style/kriya_sequence_time_stamps_and_rep_styles"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/crash_course_banner" />

                </androidx.constraintlayout.widget.ConstraintLayout>
            </com.google.android.material.card.MaterialCardView>

            <com.google.android.gms.ads.AdView
                android:id="@ id/crash_course_card_below_ad"
                style="@style/banner_ad_styles"
                ads:adSize="BANNER"
                ads:adUnitId="@string/crash_course_card_below_ad_unit_id"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/crash_course_card" />

            <com.google.android.material.card.MaterialCardView
                android:id="@ id/prep_card"
                style="@style/kriya_card_dimensions"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/crash_course_card_below_ad">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@ id/prep_card_child"
                    style="@style/kriya_card_styles">

                    <com.google.android.material.textview.MaterialTextView
                        android:id="@ id/prep_banner"
                        style="@style/kriya_card_header_styles"
                        android:contentDescription="@string/prep_aasans_card_header"
                        android:text="@string/prep_aasans_card_header"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />


                    <androidx.appcompat.widget.LinearLayoutCompat
                        android:id="@ id/kite_aasan_row"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:visibility="gone"
                        android:weightSum="3"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@ id/prep_banner">

                        <com.google.android.material.textview.MaterialTextView
                            android:id="@ id/kite_aasan"
                            style="@style/prep_aasans_style"
                            android:layout_weight="2.5"
                            android:contentDescription="@string/kite"
                            android:text="@string/kite" />

                        <androidx.appcompat.widget.AppCompatImageView
                            android:id="@ id/butterfly_done"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_weight="0.5" />

                    </androidx.appcompat.widget.LinearLayoutCompat>

                    <androidx.appcompat.widget.LinearLayoutCompat
                        android:id="@ id/rock_your_baby_row"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:visibility="gone"
                        android:weightSum="3"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@ id/kite_aasan_row">

                        <com.google.android.material.textview.MaterialTextView
                            android:id="@ id/rock_your_baby_aasan"
                            style="@style/prep_aasans_style"
                            android:layout_weight="2.5"
                            android:contentDescription="@string/sisupala"
                            android:marqueeRepeatLimit="marquee_forever"
                            android:text="@string/sisupala" />

                        <androidx.appcompat.widget.AppCompatImageView
                            android:id="@ id/rock_your_baby_done"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_weight="0.5" />
                    </androidx.appcompat.widget.LinearLayoutCompat>

                    <androidx.appcompat.widget.LinearLayoutCompat
                        android:id="@ id/naadi_vibhajana_row"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:visibility="gone"
                        android:weightSum="3"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@ id/rock_your_baby_row">

                        <com.google.android.material.textview.MaterialTextView
                            android:id="@ id/naadi_vibhajana"
                            style="@style/prep_aasans_style"
                            android:layout_weight="2.5"
                            android:contentDescription="@string/naadi_vibhajana"
                            android:text="@string/naadi_vibhajana" />

                        <androidx.appcompat.widget.AppCompatImageView
                            android:id="@ id/naadi_vibhajana_done"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_weight="0.5" />

                    </androidx.appcompat.widget.LinearLayoutCompat>

                </androidx.constraintlayout.widget.ConstraintLayout>

            </com.google.android.material.card.MaterialCardView>

            <com.google.android.gms.ads.AdView
                android:id="@ id/prep_card_below_ad"
                style="@style/banner_ad_styles"
                ads:adSize="BANNER"
                ads:adUnitId="@string/prep_card_below_ad_unit_id"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/prep_card" />

            <com.google.android.material.card.MaterialCardView
                android:id="@ id/surya_kriya_card"
                style="@style/kriya_card_dimensions"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/prep_card_below_ad">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@ id/surya_kriya_card_child"
                    style="@style/kriya_card_styles">

                    <com.google.android.material.textview.MaterialTextView
                        android:id="@ id/surya_kriya_banner"
                        style="@style/kriya_card_header_styles"
                        android:contentDescription="@string/surya_kriya_card_header"
                        android:text="@string/surya_kriya_card_header"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <com.google.android.material.floatingactionbutton.FloatingActionButton
                        android:id="@ id/shambhavi_resume_btn"
                        style="@style/floating_play_buttons"
                        android:contentDescription="@string/resume_shambhavi_practice"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/surya_kriya_banner" />

                    <androidx.appcompat.widget.AppCompatImageView
                        android:id="@ id/surya_kriya_done"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="12dp"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/surya_kriya_banner" />

                </androidx.constraintlayout.widget.ConstraintLayout>
            </com.google.android.material.card.MaterialCardView>


            <com.google.android.gms.ads.AdView
                android:id="@ id/surya_kriya_card_below_ad"
                style="@style/banner_ad_styles"
                ads:adSize="BANNER"
                ads:adUnitId="@string/surya_kriya_card_below_ad_unit_id"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/surya_kriya_card" />



            <com.google.android.material.card.MaterialCardView
                android:id="@ id/sukha_kriya_card"
                style="@style/kriya_card_dimensions"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/surya_kriya_card_below_ad">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@ id/sukha_kriya_card_child"
                    style="@style/kriya_card_styles">

                    <com.google.android.material.textview.MaterialTextView
                        android:id="@ id/sukha_kriya_banner"
                        style="@style/kriya_card_header_styles"
                        android:contentDescription="@string/sukha_kriya_card_header"
                        android:text="@string/sukha_kriya_card_header"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />


                    <androidx.appcompat.widget.AppCompatImageView
                        android:id="@ id/sukha_kriya_done"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="12dp"
                        android:visibility="gone"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent" />

                    <com.google.android.material.textview.MaterialTextView
                        android:id="@ id/sukha_kriya_duration"
                        style="@style/kriya_sequence_time_stamps_and_rep_styles"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@ id/sukha_kriya_banner" />

                </androidx.constraintlayout.widget.ConstraintLayout>
            </com.google.android.material.card.MaterialCardView>

            <com.google.android.gms.ads.AdView
                android:id="@ id/sukha_kriya_card_below_ad"
                style="@style/banner_ad_styles"
                ads:adSize="BANNER"
                ads:adUnitId="@string/sukha_kriya_card_below_ad_unit_id"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/sukha_kriya_card" />

            <com.google.android.material.card.MaterialCardView
                android:id="@ id/aum_chant_card"
                style="@style/kriya_card_dimensions"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/sukha_kriya_card_below_ad">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@ id/aum_chant_card_child"
                    style="@style/kriya_card_styles">

                    <com.google.android.material.textview.MaterialTextView
                        android:id="@ id/aum_chant_banner"
                        style="@style/kriya_card_header_styles"
                        android:contentDescription="@string/aum_chanting_card_header"
                        android:text="@string/aum_chanting_card_header"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />


                    <androidx.appcompat.widget.AppCompatImageView
                        android:id="@ id/aum_chant_done"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="12dp"
                        android:visibility="gone"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent" />

                    <com.google.android.material.textview.MaterialTextView
                        android:id="@ id/aum_chant_reps"
                        style="@style/kriya_sequence_time_stamps_and_rep_styles"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@ id/aum_chant_banner" />

                </androidx.constraintlayout.widget.ConstraintLayout>
            </com.google.android.material.card.MaterialCardView>

            <com.google.android.gms.ads.AdView
                android:id="@ id/aum_chant_card_below_ad"
                style="@style/banner_ad_styles"
                ads:adSize="BANNER"
                ads:adUnitId="@string/aum_chant_card_below_ad_unit_id"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/aum_chant_card" />

            <com.google.android.material.card.MaterialCardView
                android:id="@ id/flutter_breath_card"
                style="@style/kriya_card_dimensions"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/aum_chant_card_below_ad">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@ id/flutter_breath_card_child"
                    style="@style/kriya_card_styles">

                    <com.google.android.material.textview.MaterialTextView
                        android:id="@ id/flutter_breath_card_banner"
                        style="@style/kriya_card_header_styles"
                        android:contentDescription="@string/flutter_breath_card_header"
                        android:text="@string/flutter_breath_card_header"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <androidx.appcompat.widget.AppCompatImageView
                        android:id="@ id/flutter_breath_done"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="12dp"
                        android:visibility="gone"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent" />

                    <com.google.android.material.textview.MaterialTextView
                        android:id="@ id/flutter_breath_duration"
                        style="@style/kriya_sequence_time_stamps_and_rep_styles"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@ id/flutter_breath_card_banner" />

                </androidx.constraintlayout.widget.ConstraintLayout>
            </com.google.android.material.card.MaterialCardView>

            <com.google.android.gms.ads.AdView
                android:id="@ id/flutter_breath_card_below_ad"
                style="@style/banner_ad_styles"
                ads:adSize="BANNER"
                ads:adUnitId="@string/flutter_breath_card_below_ad_unit_id"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/flutter_breath_card" />

            <com.google.android.material.card.MaterialCardView
                android:id="@ id/meditation_card"
                style="@style/kriya_card_dimensions"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/flutter_breath_card_below_ad">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@ id/meditation_card_child"
                    style="@style/kriya_card_styles">

                    <com.google.android.material.textview.MaterialTextView
                        android:id="@ id/meditation_card_banner"
                        style="@style/kriya_card_header_styles"
                        android:contentDescription="@string/meditation"
                        android:text="@string/meditation"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />


                    <androidx.appcompat.widget.AppCompatImageView
                        android:id="@ id/meditation_done"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="12dp"
                        android:visibility="gone"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent" />

                    <com.google.android.material.textview.MaterialTextView
                        android:id="@ id/meditation_duration"
                        style="@style/kriya_sequence_time_stamps_and_rep_styles"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@ id/meditation_card_banner" />

                </androidx.constraintlayout.widget.ConstraintLayout>
            </com.google.android.material.card.MaterialCardView>

            <com.google.android.gms.ads.AdView
                android:id="@ id/meditation_card_below_ad"
                style="@style/banner_ad_styles"
                ads:adSize="BANNER"
                ads:adUnitId="@string/meditation_card_below_ad_unit_id"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/meditation_card" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
</layout>

can anyone please help me in handling this problem?

Thanks in advance

CodePudding user response:

I suspect the problem is that you have different versions of this layout, some of which have views with these IDs, and some that do not. This causes view binding to generate a nullable property for that ID, since the view might not be present in whichever configuration of the layout is currently inflated.

When you assert non-nullable with !!, this will cause the NullPointerException if your current layout is one without these views. Hint: never use !! except in unit tests and quick experimental scripts if you don't like crashes. Variables generally are not defined as nullable for no reason.

If you didn't mean to generate multiple versions of your layout, search through your res directory for alternate layout files with the same name. You need to make sure you only have the default one in the layout directory, or else make sure every alternative version of your layout has all the views that you require in every version of your screen configuration.

For example, maybe you have two layout files in these directories:

res/layout/fragment_main.xml
res/layout-land/fragment_main.xml

and you only put those views in the landscape version of the layout file. If your device is in portrait mode, and you assert the views as non-nullable, it will throw the NPE because they don't exist.

  • Related