Avinash Bellari
Avinash Bellari

Reputation: 13

Compilation error while adding onClickListener for Button

I'm getting compilation errors when trying to add onClickListener for a Button in my Android application. The error is as below:

build failed 792 ms Run build 690 ms Load build 2 ms Configure build 94 ms Calculate task graph 41 ms Run tasks 550 ms null
/home/avinash/AndroidStudioProjects/JustJava
app/src/main/java
com/example/android/justjava/MainActivity.kt
Expecting member declaration
Expecting member declaration
Expecting member declaration
Expecting member declaration
Function declaration must have a name

Task :app:buildInfoGeneratorDebug

MainActivity.kt is as below:

package com.example.android.justjava

    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.Button
    import android.widget.Toast

    class MainActivity : AppCompatActivity() {

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
        }

        val order_button = findViewById<Button>(R.id.order_button)

        //set listener
        order_button.setOnClickListener {
            //Action perform when the user clicks on the button.
            Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
        }

    }

XML is as below:

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

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Quantity"
                android:textAllCaps="true"
                android:layout_marginBottom="16dp"
                android:paddingLeft="16dp"
                android:paddingTop="16dp"/>
        <TextView
                android:id="@+id/quantity_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textColor="#000000"
                android:textSize="16sp"
                android:paddingLeft="16dp"/>
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Order"
                android:layout_marginTop="16dp"
                android:id="@+id/order_button"/>

Upvotes: 1

Views: 838

Answers (2)

Wesely
Wesely

Reputation: 1475

Since your already using Kotlin, how about trying the More Kotlin style?

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btnLabel.setOnClickListener { Toast.makeText(this, "Toast", Toast.LENGTH_SHORT).show() }
    }

You don't need findViewById() or anything else. You'll need to import something like import kotlinx.android.synthetic.main.activity_main.*

but AndroidStudio can auto generate that for you.

Upvotes: 2

Linh
Linh

Reputation: 61039

Update your MainActivity like this

class MainActivity: AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle ? ) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val order_button = findViewById<Button> (R.id.order_button)

        //set listener
        order_button.setOnClickListener {
            //Action perform when the user clicks on the button.
            Toast.makeText(this @MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
        }
    }


}

Upvotes: 1

Related Questions