Kotlin问题记录1:kotlin.NotImplementedError: An operation is not implemented: not implemented

本文介绍了在Kotlin中实现RecyclerView.Adapter时遇到的一个常见错误——未实现的方法导致应用崩溃的问题。通过具体示例展示了如何避免使用TODO注释不当引发的异常,并提供了正确的实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

kotlin.NotImplementedError: An operation is not implemented: not implemented

06-21 15:01:54.197  3104  3104 D AndroidRuntime: Shutting down VM
06-21 15:01:54.197  1239  2629 W ActivityManager:   Force finishing activity com.example.huanghongfa.kotlinnewsdemo/.MainActivity
06-21 15:01:54.197  3104  3104 E AndroidRuntime: FATAL EXCEPTION: main
06-21 15:01:54.197  3104  3104 E AndroidRuntime: Process: com.example.huanghongfa.kotlinnewsdemo, PID: 3104
06-21 15:01:54.197  3104  3104 E AndroidRuntime: kotlin.NotImplementedError: An operation is not implemented: not implemented
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at com.example.huanghongfa.kotlinnewsdemo.adapter.NewsListAdapter.getItemCount(NewsListAdapter.kt:23)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3493)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3310)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.View.layout(View.java:17972)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5814)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.View.layout(View.java:17972)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5814)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.View.layout(View.java:17972)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5814)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.View.layout(View.java:17972)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5814)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.View.layout(View.java:17972)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5814)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.View.layout(View.java:17972)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5814)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:3144)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.View.layout(View.java:17972)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5814)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2668)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2369)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1439)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7422)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:695)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:631)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.os.Looper.loop(Looper.java:158)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:7238)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
06-21 15:01:54.197  3104  3104 E AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
06-21 15:01:54.197  1239  2629 D FocusedStackFrame: Set to : 0
为什么会出现这样的Bug呢?

原来在Kotlin里面,当继承一个RecyclerView.Adapter的抽象类时,需要去实现它的一些抽象方法,不多说,直接上代码


class NewsListsAdapter(val list: ArrayList<NewsEntity>, val itemClickListener: (NewsEntity) -> Unit) : RecyclerView.Adapter<NewsListAdapter.ViewHolder>()  {
    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): NewsListAdapter.ViewHolder {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getItemCount(): Int {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onBindViewHolder(holder: NewsListAdapter.ViewHolder?, position: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}

不知道你们有没有注意到方法里面第一行代码-- TODO ,可能在Android里面加个TODO并不会影响程序运行,可是在Kotlin里面就不一样啦,如果你在某个函数的第一行添加TODO的话,那么很抱歉,它不会跳过,然后运行下一行代码。那如果真要添加TODO的话,那就只能在函数的最后一行添加了。

知道原理后就好办了,我们只需要把TODO("not implemented") 这句话去掉就可以啦!

转载于:https://my.oschina.net/u/3544269/blog/1554443

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值