I am working on a project on Android Studio. I have the main activity inside a kotlin file (MainActivity.kt) and from it I am trying to call a static method(start_netClient()) from inside a java file (dists.java) via an Intent object by putting this listener for my bbutton inside the onCreate() of the MainActivity :
bbutton.setOnClickListener(object: View.OnClickListener {
override fun onClick(view: View): Unit {
// Handler code here.
val intent = Intent(this, Net_Activity::class.java)
startActivity(intent)
}
})
The Net_Activity and its content:
class Net_Activity: AppCompatActivity() {
fun main(args: Array<String>) {
dists.start_netClient()
}
}
What I get for this code during the build is this error on Intent on the MainActivity.kt:
Error:(65, 30) None of the following functions can be called with the arguments supplied:
public constructor Intent(p0: Context!, p1: Class<*>!) defined in android.content.Intent
public constructor Intent(p0: String!, p1: Uri!) defined in android.content.Intent
I am relatively new to the whole Android thing, but what is it that I am missing, and how is it possible to connect a java method and a kotlin class via the Intent? Is it best effort to use two Activities or is there a different approach?
It seems that when you use this, you're referencing the listener object (View.OnClickListener) rather than your activity. You should be able to fix this by using this@Net_Activity, which references your activity instead of the listener. The full line should look like so:
val intent = Intent(this@Net_Activity, Net_Activity::class.java)
When you declare an object in Kotlin (via the object keyword) you're defining an anonymous class, and then creating a single instance of this class. This allows you to override methods for things like listeners, but as you've discovered, it means you have to double check your uses of this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With