类型不匹配:推断的类型是FragmentActivity?但是期望上下文

时间:2018-11-08 18:26:08

标签: android android-studio kotlin-android-extensions

我想用kotlin在tablayout中插入菜单gridview。我一直在Google中搜索很多参考文献,但它无济于事,在adapter = FoodAdapter(this, foodsList)中仍然出现1错误。它说:“类型不匹配:将类型推断为fragmentHome,但需要上下文”。这是我的fragmentHome.kt的代码

package com.example.ako.nextbrilliantgeneration

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.content.Context
import android.support.v4.app.FragmentActivity
import kotlinx.android.synthetic.main.fragment_fragment_home.*
import kotlinx.android.synthetic.main.menu_entry.view.*

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class fragmentHome : Fragment() {


var adapter: FoodAdapter? = null
var foodsList = ArrayList<Menu>()


override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?


): View? {

    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_fragment_home, container, false)

    foodsList.add(Menu("Coffee", R.drawable.profile))
    foodsList.add(Menu("Espersso", R.drawable.profile))
    foodsList.add(Menu("French Fires", R.drawable.profile))
    foodsList.add(Menu("Honey",R.drawable.profile))
    foodsList.add(Menu("Strawberry", R.drawable.profile))
    foodsList.add(Menu("Sugar cubes", R.drawable.profile))
    adapter = FoodAdapter(this, foodsList)

    gvFoods.adapter = adapter


}

class FoodAdapter : BaseAdapter {
    var foodsList = ArrayList<Menu>()
    var context: Context? = null


    constructor(context: Context, foodsList: ArrayList<Menu>) : super() {
        this.context = context
        this.foodsList = foodsList
    }

    override fun getCount(): Int {
        return foodsList.size
    }

    override fun getItem(position: Int): Any {
        return foodsList[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val food = this.foodsList[position]

        var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var foodView = inflator.inflate(R.layout.menu_entry, null)
        foodView.imgFood.setImageResource(food.image!!)
        foodView.tvName.text = food.name!!

        return foodView
    }
}

}

有什么解决办法吗?

谢谢

4 个答案:

答案 0 :(得分:2)

该错误非常清楚,您尝试传递引用this的{​​{1}},但是适配器需要Fragment object,然后尝试传递Context的{​​{1}}。

替换此行

Activity

使用

extends from Context

更新

  

调用Java get和set方法,可以使用Kotlin合成属性来替换它们。

adapter = FoodAdapter(this, foodsList)

答案 1 :(得分:1)

您的适配器输入非空 botany = get_object_or_404(Botany, pk=pk) post.botany_id = botany ,但是您传递的是Context。您可以尝试通过片段传递您的活动,或者:

Fragment

或者如果您拥有最新的支持库:

adapter = FoodAdapter(activity!!, foodsList)

您收到无法到达的错误,因为从方法中返回太早,只需将其移至底部即可。

adapter = FoodAdapter(requireActivity(), foodsList)

答案 2 :(得分:0)

这是一个很棒的更新,可以帮助使用Kotlin编写的开发人员。 getActivity方法已用@Nullable注释,以更好地支持Kotlin空安全性。以前,此方法可能返回null,因此会在Kotlin代码中导致IllegalStateException,因为在需要非null类型的地方将使用null对象。

要对用@Nullable注释的方法最好地处理这些情况,请使用let运算符将可空值展开为非空对象。

getActivity()?.let { adapter = FoodAdapter(it, foodList) }

请勿使用requireActivity()“确保”非null活动,因为如果您的活动恰好为空,则此方法将引发异常。

答案 3 :(得分:0)

在Fragment类中,您应该使用以下方式:

mFargmentSettingBinding.txtAppVersionCode.setText(Static.getAppVersion(activity!!))