3

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.

Brijesh Joshi
  • 1,817
  • 1
  • 9
  • 24
Nitish
  • 3,097
  • 13
  • 45
  • 80
  • Declare `lateinit var search_progress_bar : ProgressBar` and `search_progress_bar = search_progress_bar` – IntelliJ Amiya Jun 29 '18 at 06:03
  • Make sure your fragment is attached to the activity and is visible at the time that your callbacks are getting called. you can do so like [this](https://stackoverflow.com/a/41862150/2529309) – Kayvan N Jun 29 '18 at 06:44

3 Answers3

1

The application crashes because fetching result from API is an asynchronous operation and by the time the result is fetched (or error), the view might have been destroyed.

For example, consider the below scenario:

The API takes 5 secs to return a response or an error. So, errorListener is called after 5 secs. Within this time period, the user can press back, move to another screen etc..

In any such case, the progress bar will no longer exist as the UI has been destroyed.

To fix this, you need to check in your success and error listener whether the view/activity/fragment still exists or not and return if it does not exist.

For fragment, you can check this by using isAttached method

Udit
  • 1,037
  • 6
  • 11
1

Before you dismissing check if progressBar is null or not, if null dismiss it

  if(progressBar != null) {
      progressBar.visibility = View.GONE
  }
Jimale Abdi
  • 2,574
  • 5
  • 26
  • 33
-1

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.

Brijesh Joshi
  • 1,817
  • 1
  • 9
  • 24