Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
721 views
in Technique[技术] by (71.8m points)

kotlin - android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton

I tried 2 different AppcompatButtons and normal Button in xml, but I get the same error. Its a googlemap i just want search a city and i will go the city location

Method 1

<androidx.appcompat.widget.AppCompatButton
        app:layout_constraintLeft_toRightOf="@+id/sv_location"
        android:id="@+id/startBtn"
        android:layout_marginLeft="5dp"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:onClick="startBtn"
        android:text="add"
        tools:ignore="MissingConstraints" />

Method 2

<Button
        app:layout_constraintLeft_toRightOf="@+id/sv_location"
        android:id="@+id/startBtn"
        android:layout_marginLeft="5dp"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:onClick="startBtn"
        android:text="add"
        tools:ignore="MissingConstraints" />

iam using a searchview in button oncllick

fun startBtn(view: View){
    val searchView:SearchView =view.findViewById<SearchView>(R.id.sv_location)
    lateinit var location: String
    location = searchView.query.toString()
    var addressList: List<Address>? = null

    if (location == null || location == "") {
        Toast.makeText(requireContext(),"provide location",Toast.LENGTH_SHORT).show()
    }

    else{
        val geoCoder = Geocoder(requireContext())
        try {
            addressList = geoCoder.getFromLocationName(location, 1)

        } catch (e: IOException) {
            e.printStackTrace()
        }
        val addresss = addressList!![0]
        val latLng = LatLng(addresss.latitude, addresss.longitude)
        nMap!!.addMarker(MarkerOptions().position(latLng).title(location))
        nMap!!.animateCamera(CameraUpdateFactory.newLatLng(latLng))
        Toast.makeText(requireContext(), addresss.latitude.toString() + " " + addresss.longitude, Toast.LENGTH_LONG).show()
    }


    searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
        override fun onQueryTextSubmit(p0: String?): Boolean {
            searchView.clearFocus()

            return false
        }

        override fun onQueryTextChange(p0: String?): Boolean {
            TODO("Not yet implemented")

            return false
        }

    })

}

Fails image you cant see the exception from this link


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The reason this is not working, is because in both examples of xml, you have an onClick attribute which points to a method "startBtn". This method is not defined in your activity. Once you define it, that will tell the button what it should do once it's clicked. Here is an example of the method:

fun startBtn(v:View) {
  Toast.makeText(this, "Clicked on Button", Toast.LENGTH_LONG).show()
}

Define this method, in whichever activity/fragment the button is in.

For your second issue, make sure you are importing this:

import androidx.appcompat.widget.SearchView;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...