代码之家  ›  专栏  ›  技术社区  ›  Quick learner

Kotlin.NotImplementedError:操作未实现:未实现ImageButton中的错误单击

  •  14
  • Quick learner  · 技术社区  · 6 年前

    获取此错误

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

    我正在实现ImageButton单击侦听器

    要求 :-我想对ImageButton单击执行操作,但得到上面提到的错误

    更正我的错误,如果还有其他工作要实现ImageButton Click Listener,请务必提供,谢谢。

    这是 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>
    
    3 回复  |  直到 5 年前
        1
  •  49
  •   hluhovskyi    6 年前

    只需移除 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) {
            ...
        }
    }
    
        2
  •  3
  •   Cililing    6 年前

    TODO() 是Kotlin中的内联函数。它总是抛出NotImplementederRor。参见文档: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-t-o-d-o.html

    如果您只想标记这段代码仍然需要工作,请使用 // TODO . 然后,标记将在todo节中可见,但不会引发异常。

        3
  •  1
  •   Shaon    6 年前

    我实现了这个

      val extraTime = arrayListOf<String>("1 hour")
        val extraTimeAdapter = CustomSpinDeliveryExtraTimeAdapter(context!!, R.layout
                .simple_spinner_text_middle_down_arrow, extraTime)
        spinCustomTime.adapter = extraTimeAdapter
        spinCustomTime.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }
    
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }
    
        }
    

    从下面的代码中删除TODO后

     spinCustomTime.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
        override fun onNothingSelected(parent: AdapterView<*>?) {
    
        }
    
        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
    
        }
    
    }
    

    解决了我的问题。

    另请参阅此文档链接以获取说明 参见文档: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-t-o-d-o.html