为什么我不能在 Kotlin 中初始化我的 ArrayAdapter?

Why can't I initialise my ArrayAdapter in Kotlin?

我想使用 counties 中的字符串数组初始化 ArrayAdapter,但是当我 运行 我的应用程序时收到一条错误消息。这是我的代码:

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)

        var mContext: Context? = null
        mContext = getActivity() as CountriesActivity
        mContext.initializeCustomActionBar(R.drawable.england_flag, R.string.title_counties)
        var counties : Array<String>

        val docRef = db.collection("UKSites").document("England")
        docRef.get()
            .addOnSuccessListener { document ->
                if (document != null) {
                    counties = document.get("Counties") as Array<String>

                    adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, counties)


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

        return root
    }

}

我收到以下错误消息:

None of the following functions can be called with the arguments supplied:
public constructor ArrayAdapter<T : Any!>(@NonNull p0: Context, p1: Int, @NonNull p2: Array<(out) String!>) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(@NonNull p0: Context, p1: Int, p2: Int) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(@NonNull p0: Context, p1: Int, @NonNull p2: (Mutable)List<String!>) defined in android.widget.ArrayAdapter

似乎您的代码没有提供正确的参数,如果您使用的是 androidx.fragment.app.Fragment,您只需调用 requireContext()requireActivity()

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)

        val docRef = db.collection("UKSites").document("England")
        docRef.get()
            .addOnSuccessListener { document ->
                if (document != null) {
                    val counties = document.get("Counties") as Array<String>

                    adapter = ArrayAdapter(requireContext(), android.R.layout.simple_list_item_1, counties)


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

        return root
    }


仅供参考,从 Fragment -> Activity

转换 and/or 调用方法是非常糟糕的做法
(requireActivity() as CountriesActivity).initializeCustomActionBar(R.drawable.england_flag, R.string.title_counties)

您应该 initializeCustomActionBar 在您的 activity 上,或者让它实现一个片段随后可以调用的接口(通过构造函数)。

但我不会太担心,因为它超出了你的问题范围。