为什么不能在我的Android Kotlin应用程序中使进度条微调器不可见?

问题描述 投票:-1回答:2

我有一个片段,其中包含用于打开和关闭进度条微调器的代码,但微调器在应有的情况下不会关闭。我的XML文件包含一个列表视图和微调框。

这是我的片段代码:

package com.riverstonetech.gositeuk.ui.england

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ProgressBar
import androidx.fragment.app.Fragment
import com.google.firebase.firestore.FirebaseFirestore
import com.riverstonetech.gositeuk.CountiesActivity
import com.riverstonetech.gositeuk.CountriesActivity
import com.riverstonetech.gositeuk.MainActivity
import com.riverstonetech.gositeuk.R
import kotlinx.android.synthetic.main.fragment_england.*

class EnglandFragment : Fragment() {

    // Access a Cloud Firestore instance from your Activity
    val db = FirebaseFirestore.getInstance()
    lateinit var adapter: ArrayAdapter<String>

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_england, container, false)

        (requireActivity() as CountriesActivity).initializeCustomActionBar(
            R.drawable.england_flag,
            R.string.title_counties
        )

        var counties: ArrayList<String>

        val docRef = db.collection("UKSites").document("England")

        val progressBar = activity!!.findViewById<ProgressBar>(R.id.countiesLoadingProgressBar)

        docRef.get()
            .addOnSuccessListener { document ->

                progressBar?.visibility = ProgressBar.VISIBLE

                if (document != null) {

                    counties = document.get("Counties") as ArrayList<String>

                    adapter = ArrayAdapter(requireContext(), R.layout.list_item, counties)

                    countiesListView.adapter = adapter

                    countiesListView.setOnItemClickListener { parent, view, position, id ->

                        val intent = Intent(activity!!, CountiesActivity::class.java)
                        intent.putExtra("COUNTY", counties[position])
                        startActivity(intent)

                    }

                    progressBar?.visibility = ProgressBar.GONE

                } else {
                    Log.d("Debug", "No such document")
                }
            }
            .addOnFailureListener { exception ->
                Log.d("Debug", "get failed with ", exception)
            }


        return root
    }

}

这是我的XML代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/countiesListView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="60dp"
        android:headerDividersEnabled="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </ListView>

    <ProgressBar
        android:id="@+id/countiesLoadingProgressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:indeterminateTint="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

我感觉与列表视图的存在以及我有一个片段而不是一个活动有关。

android android-fragments kotlin android-progressbar
2个回答
0
投票

改为使用val progressBar: ProgressBar = root.findViewById<ProgressBar>(R.id.countiesLoadingProgressBar)


0
投票

问题在这里:

val progressBar = activity!!.findViewById<ProgressBar>(R.id.countiesLoadingProgressBar)

这将在活动的视图层次结构中搜索进度条。由于活动未托管此视图,因此返回值将为null。此null不会导致崩溃,因为在实际尝试访问它时会使用null处理:

progressBar?.visibility = ProgressBar.VISIBLE

如果您改为这样写:

val progressBar: ProgressBar = root.findViewById(R.id.countiesLoadingProgressBar)

您有两个好处:

  1. 您正在搜索Fragment's视图层次结构中的进度条(因此您实际上可以找到它)。
  2. 通过显式指定类型(: ProgressBar),您声明返回值永远不会为null。如果是这样,您的应用程序将崩溃。但是我们(作为人类)知道视图在片段中,所以这永远不会失败。这很有用,因为这意味着,如果您以某种方式发生崩溃,则问题出在哪里就非常明显(root不包含ID为countiesLoadingProgressBar的视图)。

您可以在此处阅读有关此显式键入的更多信息:https://kotlinlang.org/docs/reference/java-interop.html#null-safety-and-platform-types

© www.soinside.com 2019 - 2024. All rights reserved.