android - kotlin.NotImplementedError: An operation is not implemented: not implemented Error from ImageButton Click

标签 android kotlin fragment onclicklistener kotlin.notimplementederror

得到这个错误

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

我正在实现一个 ImageButton 点击​​监听器

要求 :- 我想对 imagebutton click 执行操作,但出现上述错误

更正我,如果还有其他方法可以实现 imagebutton 点击​​监听器,请提供它,谢谢

这里是 fragment java 类

class FragmentClass : Fragment(), View.OnClickListener {
    override fun onClick(v: View?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        when (v?.id) {
            R.id.back_icon -> {
                Toast.makeText(activity, "back button pressed", Toast.LENGTH_SHORT).show()
                activity.onBackPressed()
            }

            else -> {
            }
        }
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val view: View = inflater!!.inflate(R.layout.fragment_class, container,
                false)
        val activity = getActivity()
        var input_name = view.findViewById(R.id.input_name) as EditText
        var tv_addbucket = view.findViewById(R.id.tv_addbucket) as TextView
        val back_icon: ImageButton = view.findViewById(R.id.back_icon)
        back_icon.setOnClickListener(this)

        tv_addbucket.setOnClickListener(View.OnClickListener {
            Toast.makeText(activity, input_name.text, Toast.LENGTH_SHORT).show()
        })


        return view;
    }


}

然后是 fragment_class。 xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:clickable="true"
        android:padding="10dp">

        <ImageButton
            android:id="@+id/back_icon"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="#0000"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:clickable="true"
            android:src="@drawable/back_icon" />

        <TextView
            android:id="@+id/tv_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="Add Bucket" />
    </RelativeLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/header"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"

            android:layout_marginTop="?attr/actionBarSize"
            android:orientation="vertical"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="60dp">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/input_layout_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/input_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Bucket Name"
                    android:singleLine="true" />
            </android.support.design.widget.TextInputLayout>


            <TextView
                android:id="@+id/tv_addbucket"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="40dp"
                android:background="@drawable/blue_stroke_background"
                android:gravity="center"
                android:padding="15dp"
                android:text="Add"
                android:textColor="@color/white" />


        </LinearLayout>
    </ScrollView>

</RelativeLayout>

最佳答案

只需从您的 onClickListener 中删除 TODO( ... ):

override fun onClick(v: View?) {
    // No TODO here
    when (v?.id) {
        ...
    }
}

TODO(...) 是 Kotlin 函数,它总是抛出 NotImplementedError。如果您想用 TODO 标记某些内容但不抛出异常 - 只需使用带有注释的 TODO:

override fun onClick(v: View?) {
    //TODO: implement later
    when (v?.id) {
        ...
    }
}

关于android - kotlin.NotImplementedError: An operation is not implemented: not implemented Error from ImageButton Click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50503780/

相关文章:

java - 从无法扩展 Activity 的包中启动 Intent

java - Spring Boot Freemarker 从 2.2.0 升级失败

layout - 如何正确地将多个片段添加到片段过渡?

android - ListView 内部 fragment 错误

android - 错误 :Unable to resolve dependency for ':app@debug/compileClasspath' : Could not resolve com. android.support :appcompat-v7:26. 0.0-beta1

java - 如何让 MediaPlayer 的声音继续播放?不循环

java - 如何检查Android中是否存在数据库?

android - 如何在 android jetpack compose 中相互重叠列表项?

properties - Observable 属性允许在运行时添加观察者

android - 使用 AsyncTask 序列化执行多个 fragment