Kotlin中TODO("not implemented")报错

2020-02-25  本文已影响0人  简书_大叔

首次使用Kotlin语言,创建了一个点击事件,结果以运行报错了,什么鬼。错误如下:

02-25 15:09:48.629 4101-4101/com.tgh.project.mykotlin E/AndroidRuntime: FATAL EXCEPTION: main
kotlin.NotImplementedError: An operation is not implemented: not implemented
at com.tgh.project.mykotlin.MainActivity.initView(MainActivity.kt:27)
at com.tgh.project.mykotlin.MainActivity.onCreate(MainActivity.kt:22)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access600(ActivityThread.java:141) at android.app.ActivityThreadH.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

根据错误的提示kotlin.NotImplementedError: An operation is not implemented: not implemented,说自己没有实现此方法,但代码确实是实现了的,代码如下

 private fun initView() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        button_first.setOnClickListener(object:View.OnClickListener {
            override fun onClick(p0: View?) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                Toast.makeText(this@MainActivity, "点击了按钮", Toast.LENGTH_LONG).show()
            }

        })
    }

查看了官方TODO接口,

fun TODO():
Always throws NotImplementedError stating that operation is not implemented.

说此方法总是抛出异常,它的作用就是提示用户尽快实现此接口或方法,起个催促的作用,解决的办法就是删除代码中TODO("not implemented")就行了
改后如下

 private fun initView() {
        button_first.setOnClickListener(object:View.OnClickListener {
            override fun onClick(p0: View?) {
                Toast.makeText(this@MainActivity, "点击了按钮", Toast.LENGTH_LONG).show()
            }
        })
    }
上一篇下一篇

猜你喜欢

热点阅读