Aris Tsimidakis
Aris Tsimidakis

Reputation: 3

Log.d problems in kotlin (Android Studio)

So I am trying to create the flappy bird game using kotlin language, and I ran into a problem with the log.d command. Here is my MainActivity.kt code:

package com.example.flappybird

import android.os.Bundle
import android.util.Log
import android.widget.ImageButton
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private val Tag = "MainActivity"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val ImageButton: ImageButton = findViewById(R.id.btnPlay)
        ImageButton.setOnClickListener(
            Log.d(Tag)
        )
    }
}

Under the Log.d(Tag) line, I get this error:

None of the following functions can be called with the arguments supplied.
d(String?, String) defined in android.util.Log
d(String?, String?, Throwable?) defined in android.util.Log 

Now, here is my PlayThread.kt code, which is a class:

package Thread

    import Model.BackgroundImage
    import android.content.res.Resources
    import android.graphics.Canvas
    import android.util.Log
    import android.view.SurfaceHolder
    
    
    class PlayThread : Thread {
    
        private val TAG : String = "PlayThread"
        private var holder : SurfaceHolder
        private var resources : Resources
        private var isRunning : Boolean = false  //flag run or stop
        private val FPS : Int = (1000.0/60.0).toInt()  //time per frame for 60 fps
        private val backgroundImage = BackgroundImage() //object model
        private var startTime : Long = 0
        private var frameTime : Long = 0
    
    
    
        constructor(holder: SurfaceHolder, resources: Resources) {
            this.holder = holder
            this.resources = resources
            isRunning = true
        }
    
        override fun run() {
            Log.d(TAG, msg: "Thread Started")
            while(isRunning) {
                if (holder == null) return
                startTime = System.nanoTime()
                val canvas = holder.lockCanvas()
                if (!canvas) {
                    try{
                        synchronized(holder) {
                            render(canvas)
                        }
    
                    }
                finally {
                    holder.unlockCanvasAndPost(canvas)
    
                }
                }
                }
    
            }

I get this error on the Log.d(TAG, msg: "Thread Started") line:

Unexpected tokens (use ';' to separate expressions on the same line)

I dont know how to fix these, so any help would be much appreciated!

Upvotes: 0

Views: 3389

Answers (3)

Pratik Chauhan
Pratik Chauhan

Reputation: 16

 public static final String TAG=" Activity";


  val ImageButton:ImageButton = ImageButton(context)
        ImageButton.setOnClickListener{
            Log.d(TAG,"Hello World")
        }

This will make it work you have used a method that is not available by Android

Upvotes: 0

Varsha Kulkarni
Varsha Kulkarni

Reputation: 1574

Replace

Log.d(Tag)

With

Log.d(Tag, " Some log message")

This

Log.d(TAG, msg: "Thread Started")

With

Log.d(TAG, "msg: Thread Started")
//Or
Log.d(TAG, msg="Thread Started")

Upvotes: 1

Hamid Sj
Hamid Sj

Reputation: 1023

You should use:

Log.d(TAG, msg = "Thread Started") instead of:

Log.d(TAG, msg: "Thread Started")

Upvotes: 1

Related Questions