I am getting cannot create instance of abstract class in kotlin warning?

折月煮酒 提交于 2019-12-25 00:22:54

问题


I am a new to Kotlin. I have two classes, one adapter and one fragment. When I want to call the adapter class in fragment I am getting the following warning: cannot create an instance of the abstract class in Kotlin.

Below my fragment where I am getting the warning

class FavouriteExercisesFragment : BaseMvpFragment(), FavoriteExerciseView {

    var adapter: FavoriteExerciseAdapter = FavoriteExerciseAdapter()
   // cannot create instance of abstract class in kotlin

Below abstract adapter class

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
    }
}

回答1:


You can't create instance of an abstract class. You should remove abstract from the class declaration, or you should create new class extending the abstract class and make instances of that class.




回答2:


From your problem i can guess that FavoriteExerciseAdapter is abstract. you need to add a new concrete class to your project for example name it MyFavoriteExerciseAdapter. MyFavoriteExerciseAdapter must extends FavoriteExerciseAdapter and overrids all abstract methods of FavoriteExerciseAdapter now instantiate MyFavoriteExerciseAdapter instead of FavoriteExerciseAdapter:

var adapter: FavoriteExerciseAdapter = MyFavoriteExerciseAdapter()// instantiate MyFavoriteExerciseAdapter


来源:https://stackoverflow.com/questions/53822629/i-am-getting-cannot-create-instance-of-abstract-class-in-kotlin-warning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!