An operation is not implemented: not implemented被坑之路[Kotlin]

吐槽一下,程序猿的辛酸史。
今天在开发新项目时,首次使用了Kotlin语言,对于之前只在纸上谈兵,未在项目中使用过的码农,很是一脸懵逼。但是迫于公司的要求,只能硬头皮冲吧。

在编写下面代码时:

holder?.addCallback(object :SurfaceHolder.Callback{
            override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {
                TODO("not implemented") //To change body of created functions use File
            }

            override fun surfaceDestroyed(holder: SurfaceHolder?) {
                TODO("not implemented") //To change body of created functions use File 
            }

            override fun surfaceCreated(holder: SurfaceHolder?) {
                TODO("not implemented") //To change body of created functions use File 
            }
        })

项目直接闪退,在查看崩溃时,报以下错误:

Process: com.example.gaozhongkui.camerademo, PID: 28490
    kotlin.NotImplementedError: An operation is not implemented: not implemented
        at com.example.gaozhongkui.camerademo.MainActivity$onCreate$1.surfaceCreated(MainActivity.kt:44)
        at android.view.SurfaceView.updateSurface(SurfaceView.java:732)
        at android.view.SurfaceView$2.onPreDraw(SurfaceView.java:150)
        at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:977)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2747)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1650)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7579)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
        at android.view.Choreographer.doCallbacks(Choreographer.java:732)
        at android.view.Choreographer.doFrame(Choreographer.java:664)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:183)
        at android.app.ActivityThread.main(ActivityThread.java:7014)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:514)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)

很是一脸懵逼,因为未写任何代码,就会导致崩溃,在经过Google才恍然大雾。我们在继承接口时,使用IDE自动帮助生成需要实现方式时,会自动添加:

 TODO("not implemented") //To change body of created functions use File | Settings | File Templates.

重点就在TODO,有这个函数,它是要求你必须实现代码块。所以只需要删除TODO(“not implemented”) 这句话就可以了。

作为一枚程序猿有一种刨根问底的精神,在查看源码时,发现以下的猫腻:

@kotlin.internal.InlineOnly
public inline fun TODO(reason: String): Nothing = throw NotImplementedError("An operation is not implemented: $reason")

原来是TODO这个函数主动抛出的问题,现在终于知道原因了。

这是官网对kotlin TODO描述地址

你可能感兴趣的:(android,Kotlin)