kotlin.NotImplementedError: Eine Operation ist nicht implementiert: nicht implementierter Fehler von ImageButton Click

Lesezeit: 4 Minuten

Benutzeravatar des Schnelllerners
Schneller Lerner

Bekomme diesen Fehler

kotlin.NotImplementedError: Eine Operation ist nicht implementiert: nicht implementiert

Ich implementiere einen ImageButton-Klick-Listener

Erfordernis :- Ich möchte eine Aktion beim Klicken auf die Bildschaltfläche ausführen, bekomme aber den oben genannten Fehler

Korrigieren Sie mich und wenn es noch andere Arbeiten gibt, um den Imagebutton-Click-Listener zu implementieren, stellen Sie ihn bereit, danke

Hier ist der fragment Java-Klasse

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;
    }


}

und dann die 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="https://stackoverflow.com/questions/50503780/@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>

Einfach entfernen TODO( ... ) von Ihrem onClickListener:

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

TODO(...) ist eine Kotlin-Funktion, die immer wirft NotImplementedError. Wenn Sie etwas mit TODO markieren möchten, aber keine Ausnahme auslösen möchten, verwenden Sie einfach TODO mit Kommentaren:

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

  • Nun, wie man dieses Todo loswird, das die IDE dort ablegt, und es ist ärgerlich.

    – Steve Moretz

    16. April 2019 um 9:01 Uhr

  • Nie daran gedacht. Vielen Dank !

    – Giedrius Šlikas

    24. April 2019 um 11:24 Uhr

  • Was ist, wenn ein Rückgabetyp erforderlich ist, aber noch keine Implementierung vorhanden ist? zB Implementierung einer Schnittstelle

    – Raffael

    27. November 2020 um 14:56 Uhr


  • @Rafael dann müssen Sie entweder den Stub-Wert zurückgeben (z. B. 0/false/null/””, override fun test(): Int = 0) oder eine Ausnahme auslösen (z override fun test(): Int = throw IllegalStateException("Not implemented yet")

    – hluhowskij

    29. November 2020 um 13:14 Uhr

Benutzeravatar von Cililing
Cililing

TODO() ist eine Inline-Funktion in Kotlin. Es wird IMMER NotImplementedError werfen. Siehe #Dokumentation: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-todo.html

Wenn Sie nur markieren möchten, dass dieses Codefragment noch bearbeitet werden muss, verwenden Sie // TODO. Dann wird die Markierung im TODO-Abschnitt sichtbar sein, aber keine Ausnahme auslösen.

Benutzeravatar von Shaon
Shaon

Dies habe ich umgesetzt

  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.
        }

    }

Nach dem Entfernen von todo aus diesem folgenden Code

 spinCustomTime.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
    override fun onNothingSelected(parent: AdapterView<*>?) {

    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

    }

}

mein Problem gelöst.

Siehe auch diesen Dokumentlink zur Erläuterung Siehe #documentation: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-todo.html

Harrys Benutzeravatar
Harry

Einfach entfernen TODO("not implemented") von deiner onClickListener:

1394800cookie-checkkotlin.NotImplementedError: Eine Operation ist nicht implementiert: nicht implementierter Fehler von ImageButton Click

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy