类型不匹配:推断类型是 FragmentActivity?但上下文是预期的
Type mismatch: inferred type is FragmentActivity? but Context was expected
我想用 kotlin 在 tablayout 中插入菜单 gridview。我一直在 google 中搜索许多参考资料,但无济于事,在 adapter = FoodAdapter(this, foodsList)
中仍然出现 1 个错误。它说 "Type missmatch : inferred type as fragmentHome but Context was expected"。这是我 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
}
}
}
有什么解决办法吗?
谢谢
错误很明显,您尝试传递 this
,它指的是 Fragment object
,但您的适配器需要 Context
,然后尝试传递 Activity
,即 extends from Context
.
替换此行
adapter = FoodAdapter(this, foodsList)
与
adapter = FoodAdapter(getActivity(), foodsList)
更新
calls to Java get and set methods that can be replaced with the use of Kotlin synthetic properties.
getActivity() --> activity
您的适配器接收 非空 Context
,但您传入的是 Fragment
。您可以尝试从片段中传入 activity,或者:
adapter = FoodAdapter(activity!!, foodsList)
或者如果您有最新的支持库:
adapter = FoodAdapter(requireActivity(), foodsList)
您收到无法访问的错误,因为您 return 太早离开方法,只需将其移至底部即可:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
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
// move this line to last
return inflater.inflate(R.layout.fragment_fragment_home, container, false)
}
这是一个很棒的更新,可以帮助使用 Kotlin 编写代码的开发人员。 getActivity
方法已用 @Nullable
注释,以更好地支持 Kotlin 空安全。以前,此方法可能 return null,因此会在 Kotlin 代码中导致 IllegalStateException
,因为在需要非 null 类型的地方会使用 null 对象。
为了最好地处理用 @Nullable
注释的方法的这些情况,请使用 let
运算符将可空对象解包为非空对象。
getActivity()?.let { adapter = FoodAdapter(it, foodList) }
不要将 requireActivity()
用于 "ensure" 非空 activity,因为如果您的 activity 确实为空,此方法将抛出异常。
在 Fragment class 中你应该使用以下方式:
mFargmentSettingBinding.txtAppVersionCode.setText(Static.getAppVersion(activity!!))
我想用 kotlin 在 tablayout 中插入菜单 gridview。我一直在 google 中搜索许多参考资料,但无济于事,在 adapter = FoodAdapter(this, foodsList)
中仍然出现 1 个错误。它说 "Type missmatch : inferred type as fragmentHome but Context was expected"。这是我 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
}
}
}
有什么解决办法吗?
谢谢
错误很明显,您尝试传递 this
,它指的是 Fragment object
,但您的适配器需要 Context
,然后尝试传递 Activity
,即 extends from Context
.
替换此行
adapter = FoodAdapter(this, foodsList)
与
adapter = FoodAdapter(getActivity(), foodsList)
更新
calls to Java get and set methods that can be replaced with the use of Kotlin synthetic properties.
getActivity() --> activity
您的适配器接收 非空 Context
,但您传入的是 Fragment
。您可以尝试从片段中传入 activity,或者:
adapter = FoodAdapter(activity!!, foodsList)
或者如果您有最新的支持库:
adapter = FoodAdapter(requireActivity(), foodsList)
您收到无法访问的错误,因为您 return 太早离开方法,只需将其移至底部即可:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
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
// move this line to last
return inflater.inflate(R.layout.fragment_fragment_home, container, false)
}
这是一个很棒的更新,可以帮助使用 Kotlin 编写代码的开发人员。 getActivity
方法已用 @Nullable
注释,以更好地支持 Kotlin 空安全。以前,此方法可能 return null,因此会在 Kotlin 代码中导致 IllegalStateException
,因为在需要非 null 类型的地方会使用 null 对象。
为了最好地处理用 @Nullable
注释的方法的这些情况,请使用 let
运算符将可空对象解包为非空对象。
getActivity()?.let { adapter = FoodAdapter(it, foodList) }
不要将 requireActivity()
用于 "ensure" 非空 activity,因为如果您的 activity 确实为空,此方法将抛出异常。
在 Fragment class 中你应该使用以下方式:
mFargmentSettingBinding.txtAppVersionCode.setText(Static.getAppVersion(activity!!))