kotlin.NotImplementedError:未实现操作:未实现ImageButton单击时出错 [英] kotlin.NotImplementedError: An operation is not implemented: not implemented Error from ImageButton Click

查看:73
本文介绍了kotlin.NotImplementedError:未实现操作:未实现ImageButton单击时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到此错误

kotlin.NotImplementedError:未实现操作:未实现

我正在实现ImageButton点击监听器

I am implementing an ImageButton click listener

要求:-我想对imagebutton click进行操作,但遇到上述错误

Requirement :- I want to perform an action on imagebutton click , but getting the above mentioned error

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

Correct me and also if there is any other work around to implement imagebutton click listener, do provide it , Thanks

这是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( ... ):

Just remove TODO( ... ) from your onClickListener:

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

TODO(...)是始终抛出NotImplementedError的Kotlin函数.如果您想用TODO标记某些东西但不抛出异常-只需在注释中使用TODO:

TODO(...) is Kotlin function which always throws NotImplementedError. If you want to mark something with TODO but to not throw exception - just use TODO with comments:

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

这篇关于kotlin.NotImplementedError:未实现操作:未实现ImageButton单击时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆