Android:使用Kotlin的Progressbar不能为null [英] Android: Progressbar must not be null using Kotlin

查看:91
本文介绍了Android:使用Kotlin的Progressbar不能为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用webService后,我在应用程序中关闭了 progressBar .但它会因这种异常而崩溃.

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

我正在使用Kotlin.这是我的布局:

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)
}

在上面的异常的errorListener search_progress_bar.visibility = GONE内部崩溃.我多次检查了分配的ID.没有得到什么是错的.

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

推荐答案

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

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.

这篇关于Android:使用Kotlin的Progressbar不能为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆