[Kotlin]kotlin.NotImplementedError: An operation is not implemented: not implemented

前言:所谓万事开头难,在使用新语言开发的时候,总是会遇到一些Bug,然而很头疼的是,Kotlin在国内还没有这么快普及,当出现Bug的时候,就算想Google一下,参考资料也是比较少的,所以为了后来者以后的便利,决定将自己在使用Kotlin开发时,遇到的Bug,原原本本记录下来,以及一个解决方案。

在使用RecyclerView.Adapter的时候出现第一个Bug - - 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的抽象类时,需要去实现它的一些抽象方法,不多说,直接上代码

/**
 * Created by huanghongfa on 2017/6/21.
 */
class NewsListsAdapter(val list: ArrayList, val itemClickListener: (NewsEntity) -> Unit) : RecyclerView.Adapter()  {
    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") 这句话去掉就可以啦!

以上就是kotlin.NotImplementedError: An operation is not implemented: not implemented的出现以及详解,如有其它情况导致报该Bug,欢迎丢砖哦!

你可能感兴趣的:([Kotlin]kotlin.NotImplementedError: An operation is not implemented: not implemented)