我是Kotlin的新手。我有两个类,一个适配器和一个片段。当我想在片段中调用适配器类时,收到以下警告:cannot create an instance of the abstract class in Kotlin
。
在我收到警告的片段下面
class FavouriteExercisesFragment : BaseMvpFragment(), FavoriteExerciseView {
var adapter: FavoriteExerciseAdapter = FavoriteExerciseAdapter()
// cannot create instance of abstract class in kotlin
在抽象适配器类之下
class FavouriteExercisesFragment : BaseMvpFragment(), FavoriteExerciseView {
var adapter: FavoriteExerciseAdapter = FavoriteExerciseAdapter()
val presenter: FavoriteExercisePresenter = FavoriteExercisePresenter(this)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
rootView = inflater.inflate(R.layout.fragment_favorite_ex, container, false)
return rootView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initToolbar()
initList()
presenter.getFavoriteExercise()
}
private fun initList() {
rvExercise.layoutManager = LinearLayoutManager(context)
rvExercise.adapter = adapter
val helper = ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP or ItemTouchHelper.DOWN, ItemTouchHelper.RIGHT) {
override fun onMove(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
adapter.onItemMove(viewHolder.adapterPosition, target.adapterPosition)
return true
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val position = viewHolder.adapterPosition
val item = adapter.data[position]
adapter.onItemDismiss(viewHolder.adapterPosition)
presenter.deleteFromFavorite(item, position)
showEmptyData(adapter.data.isEmpty())
}
})
rvExercise.addItemDecoration(object : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View?, parent: RecyclerView, state: RecyclerView.State?) {
super.getItemOffsets(outRect, view, parent, state)
val position = parent.getChildAdapterPosition(view)
if (position == 0) {
outRect.top = 58.dp
} else {
outRect.top = 0
}
}
})
helper.attachToRecyclerView(rvExercise)
adapter.helper = helper
adapter.actions = object : FavoriteExerciseAdapter.AdapterAction {
override fun playExercise(exercise: Exercise) {
val file = exercise.files.firstOrNull { it.mainFile }
val uri = file?.getFile() ?: ""
context?.startActivityFromCenter(VideoActivity.newInstance(context, uri), rootView, VideoActivity.REQUEST_CODE)
}
}
}
private fun initToolbar() {
toolbar.setOnNavigateIconClickListener { router.exit() }
toolbar.setOnToolIconClickListener { openNavDrawer() }
}
override fun setFavoriteExercises(exercises: ArrayList<Exercise>) {
showEmptyData(exercises.isEmpty())
adapter.addData(exercises)
}
override fun showDeleteMessage(item: Exercise, position: Int) {
val snack = Snackbar.make(rootView, R.string.message_remove_from_favorite, Snackbar.LENGTH_LONG)
snack.setAction(R.string.action_undo) {
presenter.addToFavorite(item)
adapter.addItemOnPosition(item, position)
showEmptyData(adapter.data.isEmpty())
}
snack.show()
}
private fun showEmptyData(show: Boolean) {
tvEmpty.visibility = if (show) View.VISIBLE else View.GONE
emptyIcon.visibility = if (show) View.VISIBLE else View.GONE
}
}
答案 0 :(得分:0)
您无法创建抽象类的实例。您应该从类声明中删除摘要,或者应该创建扩展该抽象类的新类并创建该类的实例。
答案 1 :(得分:0)
根据您的问题,我可以猜测FavoriteExerciseAdapter
是抽象的。您需要在项目中添加一个新的具体类,例如将其命名为MyFavoriteExerciseAdapter
。 MyFavoriteExerciseAdapter
必须扩展FavoriteExerciseAdapter
并覆盖FavoriteExerciseAdapter
的所有抽象方法,现在实例化MyFavoriteExerciseAdapter
而不是FavoriteExerciseAdapter
:
var adapter: FavoriteExerciseAdapter = MyFavoriteExerciseAdapter()// instantiate MyFavoriteExerciseAdapter