调用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。没有得到什么地方不对。
答案 0 :(得分:0)
应用程序崩溃是因为从API获取结果是异步操作,并且在获取结果(或错误)时,视图可能已被破坏。
例如,考虑以下情形:
API需要5秒钟才能返回响应或错误。 因此,5秒后将调用errorListener。在此时间段内,用户可以向后按,移至另一个屏幕等。
在任何情况下,进度条将不再存在,因为UI已被破坏。
要解决此问题,您需要检查成功和错误侦听器是否仍然存在视图/活动/片段,如果不存在,则返回。
对于片段,您可以使用isAttached方法进行检查
答案 1 :(得分:0)
在关闭progressBar
是否为null
之前,先检查一下null
是否为
if(progressBar != null) {
progressBar.visibility = View.GONE
}
答案 2 :(得分:-1)
在您的代码中,没有onCreateView
函数被覆盖。是否将其添加到您的片段文件中?如果您忘记了,那可能是问题所在。