Search code examples
androidkotlinillegalstateexception

Android: Progressbar must not be null using Kotlin


I am dismissing a progressBar in my app after making webService call. But it crashes with this exception.

IllegalStateException: search_progress_bar must not be null

I am using kotlin. This is my layout:

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/eco_after_payment_process">

<android.support.constraint.ConstraintLayout
    android:id="@+id/searching_technician_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:visibility="visible"
    android:orientation="vertical"
    android:gravity="center"
    android:paddingStart="40dp"
    android:paddingEnd="40dp">

    <in.customviews.RubikRegularTextView
        android:id="@+id/searching_techinician"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/searching_eco_ninja"
        android:gravity="center"
        android:layout_marginTop="@dimen/margin_twenty"
        app:layout_constraintTop_toTopOf="parent"
        android:visibility="visible"/>

    <ProgressBar
        android:id="@+id/search_progress_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:indeterminateOnly="true"
        android:scaleY="1.5"
        android:indeterminateBehavior="repeat"
        app:layout_constraintTop_toBottomOf="@id/searching_techinician"
        android:visibility="visible"/>

    <ImageView
        android:id="@+id/ninja_face"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:src="@drawable/ninja_face"
        app:layout_constraintTop_toBottomOf="@id/search_progress_bar"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:padding="50dp"/>

</android.support.constraint.ConstraintLayout>

<android.support.constraint.ConstraintLayout
    android:id="@+id/assigned_successfully_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:visibility="gone">


    <ImageView
        android:id="@+id/check_icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/check_icon_green"
       app:layout_constraintBottom_toTopOf="@id/assigned_successfully_message"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginBottom="@dimen/margin_ten"/>

    <in.customviews.RubikRegularTextView
        android:id="@+id/assigned_successfully_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/ninja_assigned_successfully"
        android:gravity="center"
        android:layout_marginTop="40dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:visibility="visible"
        android:textSize="@dimen/font_fifteen"/>

</android.support.constraint.ConstraintLayout>

<ScrollView
    android:id="@+id/assignment_failed_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:visibility="gone">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ProgressBar
            android:id="@+id/time_slot_progress_bar_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="?android:attr/progressBarStyleHorizontal"
            android:indeterminateOnly="true"
            android:scaleY="1.5"
            android:indeterminateBehavior="repeat"
            app:layout_constraintTop_toTopOf="parent"
            android:visibility="gone"/>
    </android.support.constraint.ConstraintLayout>

</ScrollView>
</android.support.constraint.ConstraintLayout>

I have this import in fragment class:

import kotlinx.android.synthetic.main.eco_after_payment_process.*

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

    search_progress_bar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(search_progress_bar.context, R.color.black), PorterDuff.Mode.SRC_IN)
    search_progress_bar.getProgressDrawable().setColorFilter(ContextCompat.getColor(search_progress_bar.context, R.color.black), PorterDuff.Mode.SRC_IN)
    val animator = AnimatorInflater.loadAnimator(context, R.animator.ninja_animator)
    animator.setTarget(ninja_face)
    animator.start()
    assignTechnician()
}

private fun assignTechnician() {
    var params = JSONObject()
    try {
        params.put("order_id", order_id)
    } catch (e: JSONException) {
        e.printStackTrace()
    }
    val responseListener = Response.Listener<JSONObject> {
        val message = it.optString("Message")
        if (message.equals("Success", true)) {
            Handler().postDelayed({
                checkTechnicianAssignment()
            }, 60000)
        } else {
            search_progress_bar.visibility = View.GONE
            val snackbar = Snackbar.make(eco_after_payment_process, "Network Error.", Snackbar.LENGTH_INDEFINITE)
            snackbar.setAction("Retry", View.OnClickListener {
                snackbar.dismiss()
                assignTechnician()
            })
            snackbar.show()
        }
    }

    val errorListener = Response.ErrorListener {
        search_progress_bar.visibility = View.GONE
        val snackbar = Snackbar.make(eco_after_payment_process, "Network Error.", Snackbar.LENGTH_INDEFINITE)
        snackbar.setAction("Retry", View.OnClickListener {
            snackbar.dismiss()
            assignTechnician()
        })
        snackbar.show()
    }

    search_progress_bar.visibility = View.VISIBLE
    Api.triggerTechnicianAssignment(params, responseListener, errorListener)
}

It crashes inside errorListener search_progress_bar.visibility = GONE with above exception. I checked the assigned id multiple times. Not getting what is wrong.


Solution

  • In your code there is no onCreateView function overriden. Is it added in your Fragment File or not? If you forgot that than this might be the issue.