Home > Software design >  My MapsActivity crashes without any error message
My MapsActivity crashes without any error message

Time:11-14

I think that the problem is I don't know to use well the Coroutines. In Maps Activity you'll see that I access to a PointsDao suspend function that returns a List of objects that I want to use to create marks at my Google Maps Activity.

@AndroidEntryPoint

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {

private lateinit var mMap: GoogleMap
private lateinit var binding: ActivityMapsBinding
private lateinit var requestPermissionLauncher: ActivityResultLauncher<Array<String>>
private val permissions = arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
private lateinit var fusedLocationClient: FusedLocationProviderClient
private val mapsViewModel: MapsViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMapsBinding.inflate(layoutInflater)
    setContentView(binding.root)

    requestPermissionLauncher = registerForActivityResult(
        ActivityResultContracts.RequestMultiplePermissions()
    ) {
        permissions ->
            if (permissions.getOrDefault(Manifest.permission.ACCESS_FINE_LOCATION, false)) {
                Log.d("fine_location", "Permission granted")
            } else {
                Log.d("fine_location", "Permission not granted")
                getBackToMainActivity()
                Toast.makeText(this, "Necessites acceptar els permisos de geolocalització per a realitzar la ruta", Toast.LENGTH_LONG).show()
            }
            if (permissions.getOrDefault(Manifest.permission.ACCESS_COARSE_LOCATION, false)) {
                Log.d("coarse_location", "Permission granted")
            } else {
                Log.d("coarse_location", "Permission not granted")
                getBackToMainActivity()
                Toast.makeText(this, "Necessites acceptar els permisos de geolocalització per a realitzar la ruta", Toast.LENGTH_LONG).show()
            }
    }

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    val mapFragment = supportFragmentManager
        .findFragmentById(R.id.map) as SupportMapFragment
    mapFragment.getMapAsync(this)

    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
    requestLocationPermissions()
}

/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera.
 */
override fun onMapReady(googleMap: GoogleMap) {
    mMap = googleMap
    CoroutineScope(Dispatchers.Main).launch {
        val listOfPoints = getRoutePoints()
        for (point in listOfPoints) {
            mMap.addMarker(MarkerOptions().position(LatLng( point.latitude, point.longitude)))
            if (point == listOfPoints[0]) {
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(point.latitude, point.longitude), 18f))
            }
        }
    }
}

private fun requestLocationPermissions() {
    when (PackageManager.PERMISSION_GRANTED) {
        ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) -> {
            Log.d("fine_location", "Permission already granted")
        }
        ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) -> {
            Log.d("coarse_location", "Permission already granted")
        }
        else -> {
            requestPermissionLauncher.launch(permissions)
        }
    }
}

private fun getBackToMainActivity() {
    val intent = Intent(this, MainActivity::class.java)
    startActivity(intent)
}

private fun getRouteId(): Int {
    return intent.getIntExtra("routeId", 0)
}

// Gets the points from room repository through ViewModel
private fun getRoutePoints(): List<PointOfInterest> {
    val route = getRouteId()
    var points = emptyList<PointOfInterest>()
    CoroutineScope(Dispatchers.IO).launch {
        points = mapsViewModel.getRoutePoints(route)
    }
    return points
}

This is my ViewModel for this Activity:

@HiltViewModel
class MapsViewModel @Inject constructor(private val repository: RoomRepository): ViewModel() {

    suspend fun getRoutePoints(routeId: Int): List<PointOfInterest> {
        return repository.getPointsByRouteId(routeId)
    }
}

And the Dao:

@Dao
interface PointsDao
{
    @Query("SELECT * FROM points_tbl WHERE route_id = :routeId")
    suspend fun getRoutePoints(routeId: Int): List<PointOfInterest>
}

My stracktrace error:

Process: com.buigues.ortola.touristics, PID: 27515
    java.lang.IllegalStateException: Method addObserver must be called on the main thread
        at androidx.lifecycle.LifecycleRegistry.enforceMainThreadIfNeeded(LifecycleRegistry.java:317)
        at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:172)
        at androidx.lifecycle.SavedStateHandleController.attachToLifecycle(SavedStateHandleController.java:49)
        at androidx.lifecycle.SavedStateHandleController.create(SavedStateHandleController.java:70)
        at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:67)
        at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:84)
        at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:109)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:171)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:139)
        at androidx.lifecycle.ViewModelLazy.getValue(ViewModelLazy.kt:44)
        at androidx.lifecycle.ViewModelLazy.getValue(ViewModelLazy.kt:31)
        at com.buigues.ortola.touristics.ui.MapsActivity.getMapsViewModel(MapsActivity.kt:39)
        at com.buigues.ortola.touristics.ui.MapsActivity.getRoutePoints(MapsActivity.kt:123)
        at com.buigues.ortola.touristics.ui.MapsActivity.access$getRoutePoints(MapsActivity.kt:31)
        at com.buigues.ortola.touristics.ui.MapsActivity$onMapReady$1.invokeSuspend(MapsActivity.kt:85)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)

CodePudding user response:

The problem is here in getRoutePoints().

    CoroutineScope(Dispatchers.IO).launch {
        points = mapsViewModel.getRoutePoints(route)
    }

The by viewModels() in your ViewModel property does a lazy load of the ViewModel. As a result, if you access your ViewModel property for the first time when you are not on the main thread, it will try to create it on the wrong thread, triggering this crash. ViewModels must be constructed on the main thread.

CoroutineScope(Dispatchers.IO) means you are creating a coroutine scope that by default uses background IO threads, so this code is run on a background thread.

You should not be creating a CoroutineScope for this anyway, because your Activity already has one that is properly managed by the Activity lifecycle (so it will cancel any in-progress jobs if the activity is closed, to avoid wasting resources).

Also, getRoutePoints() is a suspend function. There's no reason for you to be using Dispatchers.IO here. A suspend function by convention is safe to call from any dispatcher. (It is however possible to write one that breaks convention, but Room is properly designed and does not break convention.)

To fix the crash and run a coroutine properly, you should use lifecycleScope.launch { //.... However, this function as you have designed it won't do what you expect. It launches a coroutine to retrieve a value, but then it immediately returns before that coroutine has finished running, so in this case will just return the initial emptyList(). When you launch a coroutine, you are queuing up background work, but the current function that called launch continues synchronously without waiting for the coroutine results. If it did, it would be a blocking function. There's more information about that in my answer here.

So, you should instead make this a suspend function:

// Gets the points from room repository through ViewModel
private suspend fun getRoutePoints(): List<PointOfInterest> {
    val route = getRouteId()
    return mapsViewModel.getRoutePoints(route)
}

And your onMapReady function should also be fixed to use proper scope:

override fun onMapReady(googleMap: GoogleMap) {
    mMap = googleMap
    lifecycleScope.launch {
        val listOfPoints = getRoutePoints()
        for (point in listOfPoints) {
            mMap.addMarker(MarkerOptions().position(LatLng( point.latitude, point.longitude)))
            if (point == listOfPoints[0]) {
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(point.latitude, point.longitude), 18f))
            }
        }
    }
}
  • Related