前言:所謂萬事開頭難宫补,在使用新語言開發(fā)的時(shí)候,總是會(huì)遇到一些Bug曾我,然而很頭疼的是粉怕,Kotlin在國內(nèi)還沒有這么快普及,當(dāng)出現(xiàn)Bug的時(shí)候抒巢,就算想Google一下贫贝,參考資料也是比較少的,所以為了后來者以后的便利蛉谜,決定將自己在使用Kotlin開發(fā)時(shí)稚晚,遇到的Bug,原原本本記錄下來型诚,以及一個(gè)解決方案客燕。
在使用RecyclerView.Adapter的時(shí)候出現(xiàn)第一個(gè)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
為什么會(huì)出現(xiàn)這樣的Bug呢?
原來在Kotlin里面狰贯,當(dāng)繼承一個(gè)RecyclerView.Adapter的抽象類時(shí)也搓,需要去實(shí)現(xiàn)它的一些抽象方法赏廓,不多說,直接上代碼
/**
* Created by huanghongfa on 2017/6/21.
*/
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里面加個(gè)TODO并不會(huì)影響程序運(yùn)行楚昭,可是在Kotlin里面就不一樣啦,如果你在某個(gè)函數(shù)的第一行添加TODO的話拍顷,那么很抱歉抚太,它不會(huì)跳過,然后運(yùn)行下一行代碼昔案。那如果真要添加TODO的話尿贫,那就只能在函數(shù)的最后一行添加了。
知道原理后就好辦了踏揣,我們只需要把TODO("not implemented") 這句話去掉就可以啦庆亡!
以上就是kotlin.NotImplementedError: An operation is not implemented: not implemented的出現(xiàn)以及詳解,如有其它情況導(dǎo)致報(bào)該Bug捞稿,歡迎丟磚哦又谋!