拨打webService调用后,我在我的应用程序中关闭了 progressBar 。但是它崩溃与这个异常。

IllegalStateException: search_progress_bar must not be null

我正在用Kotlin。这是我的布局:
<?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>

我在片段类中有此导入:
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)
}

发生上述异常时,它会在errorListener search_progress_bar.visibility = GONE内部崩溃。我多次检查了分配的ID。没有得到什么是错的。

最佳答案

在您的代码中,没有onCreateView函数被覆盖。它是否已添加到您的片段文件中?如果您忘记了,那可能是问题所在。

10-07 20:03