App Crashes When Running Maps Activity

App crashes when running maps activity

The same problem here when using target SDK 28. Reverting to SDK 27 solved the problem.


Don't revert your target SDK.

Better use this solution https://stackoverflow.com/a/50944537/5710605

My MapsActivity crashes without any error message

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))
}
}
}
}

Google Maps crashes on my app when I click the button (Intent) to open it

You are getting the exception :

Trying to instantiate a class com.google.android.gms.maps.SupportMapFragment that is not a Fragment –Caused by: java.lang.ClassCastException

You are using MapActivity : GoogleMaps extends MapActivity means you need to deal with MapFragment not SupportMapFragment

You can handle this in two ways :

1.
Replace SupportMapFragment with MapFragment

<fragment
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

In this case you need to import :

import android.app.Fragment;
import android.app.FragmentTransaction;

2. Replace MapActivity with FragmentActivity

In this case you need to import :

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

app crashes with MapView if i use sdk 29 ------

Add this under <application> tag in Manifest.xml

<uses-library android:name="org.apache.http.legacy" android:required="false"/>

With Android 6.0, we removed support for the Apache HTTP client.
Beginning with Android 9, that library is removed from the
bootclasspath and is not available to apps by default.

To continue using the Apache HTTP client, apps that target Android 9
and above can add the above line to their AndroidManifest.xml:

Here is more info
by Google

Android Google maps, Moving Marker crash application

The crash is happening because you are not running this code in the UIThread.

You can use runOnUiThread and implement a second mandatory run() method for Runnable() to work with your TimerTask.

Try replacing your code within myTimer() with the following:

public void myTimer() {
Timer t = new Timer();

t.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {

@Override
public void run () {
if (mMap != null) {

x = x + 1;
y = y + 1;

NextPosition = new LatLng(x, y);

marker.setPosition(NextPosition);
mMap.moveCamera(CameraUpdateFactory.newLatLng(NextPosition));
mMap.animateCamera(CameraUpdateFactory.zoomTo(16f));
}
}
});
}
}, 2000, 1000);
}

Also see related threads:


java.lang.IllegalStateException: Not on the main thread Google Maps


Android Add Map Marker Error: java.lang.IllegalStateException: Not on the main thread

Hope this helps!



Related Topics



Leave a reply



Submit