Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FATAL EXCEPTION: DefaultDispatcher-worker-1 Process: com.techcomputerworld.examples, PID: 6394 kotlin.UninitializedPropertyAccessException: lateinit property retrofit has not been initialized at com.techcomputerworld.examples.superheroapp.SuperHeroListActivity$searchByName$1.invokeSuspend(SuperHeroListActivity.kt:67) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42) at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664) Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@84a5897, Dispatchers.IO]
- El codigo de mi clase principal:
- package com.techcomputerworld.examples.superheroapp
- import android.os.Bundle
- import android.util.Log
- import androidx.activity.enableEdgeToEdge
- import androidx.appcompat.app.AppCompatActivity
- import androidx.appcompat.widget.SearchView
- import androidx.core.view.ViewCompat
- import androidx.core.view.WindowInsetsCompat
- import com.techcomputerworld.examples.R
- import com.techcomputerworld.examples.databinding.ActivitySuperHeroListBinding
- import kotlinx.coroutines.CoroutineScope
- import kotlinx.coroutines.Dispatchers
- import kotlinx.coroutines.launch
- import retrofit2.Response
- import retrofit2.Retrofit
- import retrofit2.converter.gson.GsonConverterFactory
- /*
- * Voy a utilizar corutinas que es para utilizar otros hilos al principal para hacer llamadas a la API, tambien se usa para llamar a bases de datos
- * O tareas que tengamos que hacer sin bloquear el hilo principal y que la aplicación no se trabe o deje de funcionar bien.
- * Para trabajar con multi hilo hay varias maneras una es las corutinas y hay otras maneras.
- * */
- class SuperHeroListActivity : AppCompatActivity() {
- private lateinit var binding: ActivitySuperHeroListBinding
- private lateinit var retrofit: Retrofit
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- binding = ActivitySuperHeroListBinding.inflate(layoutInflater)
- setContentView(binding.root)
- enableEdgeToEdge()
- //setContentView(R.layout.activity_super_hero_list)
- /*ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
- val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
- v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
- insets
- }*/
- ViewCompat.setOnApplyWindowInsetsListener(binding.main) { v, insets ->
- val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
- v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
- insets
- }
- initUI()
- }
- private fun initUI() {
- binding.searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener
- {
- override fun onQueryTextSubmit(query: String?): Boolean {
- searchByName(query.orEmpty())
- return false
- }
- override fun onQueryTextChange(newText: String): Boolean {
- return false
- }
- /* se puede dejar tal que así.
- override fun onQueryTextChange(newText: String?) = false
- */
- })
- }
- private fun searchByName(query: String) {
- //todo lo que se ejecute dentro de este codigo CorutineScope, y le hemos dicho que se ejecute en otro hilo IO
- CoroutineScope(Dispatchers.IO).launch {
- val myResponse : Response<SuperHeroDataResponse> = retrofit.create(ApiService::class.java).getSuperheroes(query)
- if (myResponse.isSuccessful){
- Log.i("TCW", "funciona")
- }else {
- Log.i("TCW", "no funciona :(")
- }
- }
- }
- private fun getRetrofit(): Retrofit {
- //crear el objeto Retrofit
- return Retrofit.Builder()
- .baseUrl("https://superheroapi.com/")
- .addConverterFactory(GsonConverterFactory.create())
- .build()
- //return retrofit
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement