**How can I solved it**
// How can resolve This error from kotlin fragment *
open class First : Fragment() {
}
// 'Which is showing in image Fragment() was not access' https://i.stack.imgur.com/Rcgl5.png
**How can I solved it**
// How can resolve This error from kotlin fragment *
open class First : Fragment() {
}
// 'Which is showing in image Fragment() was not access' https://i.stack.imgur.com/Rcgl5.png
Fragments are special classes in Android and they need primary constructor (and that constructor is after name of class). This constructor should by empty (if you declare any fields then you'll see warning, that you should not create Fragments with parameters).
So, all you need to compile your code is add brackets after fragment name:
class MyFragment() : Fragment() { /* some code here! remebmer about brackets after your MyFragment! */ }
Even more, you should avoid declaring any constructors with params.
You should create your fragments by Companion.newInstance(someArgs: List<Arg>) : YourFragment
. (where Companion is companion object of your Fragment).
How fragments should be initialized you can find here: https://stackoverflow.com/a/9245510/7508302
Try this code:
class MyFragment() : Fragment() {
constructor(supportFragmentManager:FragmentManager?) : this() {
}
}
Now you have 2 constructors:
Here is full example, hope it could help. Parent class:
open class ResponseModel {
var statusCode: Int = 0
var errorMessage: String = ""
constructor()
constructor(statusCode: Int) {
this.statusCode = statusCode
}
constructor(statusCode: Int, errorMessage: String) : this(statusCode) {
this.errorMessage = errorMessage
}
}
Child class:
class Response2Model : ResponseModel {
val a:String = ""
constructor(statusCode: Int, errorMessage: String) : super(statusCode, errorMessage)
constructor(a: String) : super() {
this.a = a
}
}