1
votes

I was having the following error

Skipped 40 frames!  The application may be doing too much work on its main thread.

Researched about it... Made sure everything started in new threads as much as possible with Runnables. But kept getting the error. I almostly commented all my code and still got it when I started a new activity. Then I commented this mapfragment from my first activity and there error was gone. So the error is caused by following piece of code:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.google.android.gms.maps.MapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    />

I got this from Google Maps API for Android docs... Kind of weird it's not optimised. How can I make sure the error disappears? Can I delay the load of the mapFragment?

Once I opened the Activity once... and reopen it again the warning is gone because it's in the memory already.. but I'd like to have the best optimised app possible.

To avoid confusion It's the setContentView that is causing delay because the mapFragment is loaded in this function. The delay causes a warning to be thrown. My question: how can I speed up the setContentView, so the mapFragment is pre-loaded or loads after the activity is opened.

2
There is no code running. Not a single line. Only the load of the views, it's the fragment xml that causes the warning. If I uncomment the code then all my code is in different threads and one is in an AsyncTask so that's okay.Jesse
if you show your java code i can help youLahiru Prasanna
@LahiruPrasanna it's a closed source project. I can only show the part I'm showing right now. There is no code to show. Make a new empty project, put a mapfragment in (just the code pasted above) and you'll see that it gives too much work on main thread warnings.Jesse

2 Answers

0
votes

setContentView() slow with map fragment

This contains the answer. Was deeply down hidden in Google, but found it!

0
votes

Try like this

getMapAsync(OnMapReadyCallback callback) you can set up listener for when GoogleMap is ready

public class MainActivity extends FragmentActivity
    implements OnMapReadyCallback {  
...  
}  
@Override  
public void onMapReady(GoogleMap map) {
    map.addMarker(new MarkerOptions()
        .position(new LatLng(0, 0))
        .title("Marker"));
}