Questions tagged [kotlin-extension]

Kotlin provides the ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern such as Decorator. This is done via special declarations called extensions.

333 questions
0
votes
1 answer

Kotlin infix function shadows/hides member function

I am just trying to write some examples with Kotlin. And what I did was to create a Jersey app, and everything was going well until I try to change the Main.java class to Main.kt. The generated Main.java class has this method; public static…
mndeveci
  • 301
  • 4
  • 15
0
votes
2 answers

Kotlin Recursive Extension Function to Walk Android View Hierarchy

I am attempting to create a generic extension function that can walk an Android view hierarchy and return the first occurence of a view of a specific type. The idea would be to invoke the extension as follows (to find the first occurence of a…
Victor Rendina
  • 1,130
  • 12
  • 19
0
votes
2 answers

Why return null from custom dialog EditText?

Here's the AlertDialog inside the MainActivity onCreate method: import kotlinx.android.synthetic.main.dialog.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { …
0
votes
2 answers

OnClick and TextView in Android Studio 3

I am making a basic clicker game. For this who are not femuler: (Evertime the button is clicked it adds 1 to the value of the textView) which starts at 0 of course. This is what I have but there is an error at "as" which says this cast will never…
0
votes
1 answer

Clicking a textview to change fragment

I am trying to build my first android app using Kotlin but I am stuck on a very simple situation. I am using Kotlin android extensions and I am getting a Null pointer exception.
0
votes
0 answers

The result the same, but the test case not pass in unit test

I just touch the unit test, today encountered a very strange question, I use assertThat to determine whether the two objects are equal, there is a test case can not pass, when I test the object: sealed class SummaryViewState : MviViewState { …
littlegnal
  • 425
  • 4
  • 14
0
votes
1 answer

Extension function not creating new Observable object

I am having unexpected behavior with kotlin and rxjava. I create an extension function for loading image using picasso fun Picasso.loadBitmap(url: String) : Observable = Observable.create { emitter -> Log.d("picasso…
Hohenheim
  • 393
  • 2
  • 16
0
votes
1 answer

Kotlin extension generic function with basic types without reflection API

I have a Java object that has methods getLong, getBoolean and getString. I attempted to make a generic extension function that has a function as the last parameter. Essentially wrapping the try and catch and call to getString etc that could throw an…
0
votes
2 answers

Android Studio 3 unable to build kotlin fragment

Im trying to use Kotlin with Android Studio 3.0 with my existing project. I have created a fragment using kotlin. Im trying to use the kotlin fragment in my Java Activity. But everytime i try to run it I get Error:(209, 5) error: cannot find…
0
votes
1 answer

what is the equivalent of this java code to kotlin

what is the equivalent kotlin of the following java code: F f=new F(); convert(f, HashMap.class); This is what i have tried so far val f = F() convert(f, HashMap<*, *>::class.java) This is the error i am getting: Only classes are allowed on the…
0
votes
1 answer

Extensions in Kotlin

I want to use extensions for variables and method for my custom class. I am trying below code but getting error Local extension properties are not allowed val Double.km: Double get() = this * 1000 val Double.m: Double get() = this val Double.cm:…
Rohit Parmar
  • 367
  • 1
  • 4
  • 19
0
votes
0 answers

How to create a static class in Kotlin?

I have 2 classes: object TrimNCompressConstants { var REQUEST_CODE = 101 var EXTRA_VIDEO_PATH = "EXTRA_VIDEO_PATH" var EXTRA_MESSAGE = "EXTRA_MESSAGE" var RESULT_SUCCESS = 102 } and class VideoUtil (var context : Context?) { fun show…
GeekyInt
  • 393
  • 4
  • 9
0
votes
3 answers

While loop in Kotlin Programming Language

In my kotlin code i am getting Type mismatch(inferred type is Int but Boolean was expected) error. fun main(args: Array) { var i = args.size while (i--){ println(args[i]) } }
Rohit Parmar
  • 367
  • 1
  • 4
  • 19
0
votes
2 answers

How to keep object type for smart casting when returning Any

Apologies if this is a silly question, Kotlin is still new to me and I'm unfamiliar with syntax "types" so found it difficalt to find the solution. fun Any?.test(): Any? { return this } "test string".test() // implicit string is now type of…
ScruffyFox
  • 150
  • 2
  • 9
0
votes
1 answer

Extend ViewGroup as a fragment wrapper

I am extending a RelativeLayout to make a fragment decorator. Like this: