Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – on kotlin, textview not initialized outside onCreate method

Posted on November 11, 2022 By

Solution 1 :

You cannot instantiate Activities on Android. They are instantiated by the OS, and the OS calls the lifecycle methods on it.

In an MVP pattern, the View and Presenter both reference each other. Since Activity (the View) is the entry point of the application, your Activity should instantiate the Presenter and pass a reference of itself to the Presenter so communication can go both ways.

Also, the reference to the activity in the Presenter should be specified as a ViewInterface, not an Activity, or you’re kind of defeating the purpose of using MVP.

class SplashPresentation(private val view: Splash.ViewInterface) : Splash.PresentationInterface {

    //... methods that call functions on view
}

class SplashActivity : AppCompatActivity(), Splash.ViewInterface {

    private val presenter = SplashPresentation(this)

    //...

}

Problem :

I am new at Kotlin and trying to implement MVP Architecture,

Currently I am having problem initializing/setting textview’s value outside onCreate() method
here is my code

SplashActivity.kt

class SplashActivity : AppCompatActivity(), Splash.ViewInterface {
lateinit var appDetail: AppDetail
lateinit var textTitle: TextView

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

    textTitle = findViewById(R.id.splash_txt_title) as TextView

    AppSingleton.appContext = applicationContext

    var splashPresentation = SplashPresentation(this)
    splashPresentation.getAppDetailFromService()
}

override fun fetchAppDetailSuccessful(response: SplashServiceObject) {
    AppSingleton.initializeAppDetal(Gson().fromJson(response.json_string, AppDetail::class.java))
    this.appDetail = AppSingleton.appDetail
}

override fun fetchAppDetailFailed(errMsg: String) {
    textTitle.text = errMsg
}
}

SplashPresenter.kt

class SplashPresentation(private val view: Splash.ViewInterface) : Splash.PresentationInterface {

fun getAppDetailFromService() {
    var splashService = SplashService()
    splashService.getAppDetailFromAssets(this)
}

override fun fetchAppDetailFromServiceSuccessful(response: SplashServiceObject) {
    view.fetchAppDetailSuccessful(response)
}

override fun fetchAppDetailFromServiceFailed(errMsg: String) {
    view.fetchAppDetailFailed(errMsg)
}
}

SplashService.kt

 class SplashService {
fun getAppDetailFromAssets(splashPresentation: SplashPresentation) {
    val json_filename = "appdetail.json"
    var jsonResponse: JsonResponse = AppSingleton.commonUtils.fetchJsonFromAssets(json_filename, AppSingleton.appContext!!)
    if (jsonResponse.json_status) {
        var splashServiceObj = SplashServiceObject
        splashServiceObj.json_string = jsonResponse.json_info
        splashServiceObj.response_msg = "JSON Successful fetched."
        splashPresentation.fetchAppDetailFromServiceSuccessful(splashServiceObj)
    } else {
        splashPresentation.fetchAppDetailFromServiceFailed(jsonResponse.json_info)
    }
}
}

in my SplashActivity().onCreate(), I am calling a Presenter that access Service, then the Service return a value to Presenter,
Then Presenter, return value to my SplashActivity’s View, one of the function is, fetchAppDetailFailed(errMsg)

READ  [FIXED] java - Pass activity result to correct fragment
Powered by Inline Related Posts

when I run the app, it crashes, saying the “textaa” is not yet initialized.
back in Java exp, when the variable is already instantiated on onCreate(), you can call this variable anywhere within the activity.

Thanks in advance!

Comments

Comment posted by Anand Diamond

Hello @Machee Neraid please send me Service and Presenter code.

Comment posted by MaChee Neraid

@AnandDiamond, I updated my question with presenter and service. thanks!

Comment posted by MaChee Neraid

Thank you very much! It works! By adding the SplashPresentation(private val view: Splash.ViewInterface) rather than Context, it is now working! Thanks!

Comment posted by MaChee Neraid

btw, do you mind checking my overall code? if there’s any issues or problems? it will be a great help for newbie like me! thanks again!

Comment posted by codereview.stackexchange.com

You might consider posted it on

Android Tags:android, android-mvp, kotlin

Post navigation

Previous Post: [FIXED] java – How to solve debug issue
Next Post: [FIXED] android – How to solve setting up this payment problem – paypal

Related Posts

[FIXED] Tensorflow Object Detection Performance Drop in Android Android
[FIXED] fragment – Shapeable image view – corner color – android Android
[FIXED] android – I have uploaded to google play store an app. But it doesn’t support device Huawei and other mobile phones. What could it be? This is my manifest Android
[FIXED] android – My text widget overflows even if I use the overflow property Android
[FIXED] java – Retrofit can’t get data from firebase database Android
[FIXED] Android – Constraint Layout – Alignment of Text and Icon Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme