[android-auto] Fix null pointer exception

Signed-off-by: Andrei Shkrob <github@shkrob.dev>
This commit is contained in:
Andrei Shkrob
2025-08-03 15:05:32 +02:00
committed by Konstantin Pastbin
parent 8e0d4776af
commit a580c19dc0
3 changed files with 15 additions and 9 deletions

View File

@@ -24,9 +24,8 @@ public class MapFragment extends BaseMwmFragment implements View.OnTouchListener
{
private static final String TAG = MapFragment.class.getSimpleName();
@SuppressWarnings("NonNullFieldNotInitialized")
@NonNull
private Map mMap;
private final Map mMap = new Map(DisplayType.Device);
public void updateCompassOffset(int offsetX, int offsetY)
{
@@ -87,7 +86,8 @@ public class MapFragment extends BaseMwmFragment implements View.OnTouchListener
{
Logger.d(TAG);
super.onAttach(context);
mMap = new Map(DisplayType.Device, MwmApplication.from(requireContext()).getLocationHelper());
mMap.setLocationHelper(MwmApplication.from(requireContext()).getLocationHelper());
mMap.setMapRenderingListener((MapRenderingListener) context);
mMap.setCallbackUnsupported(this::reportUnsupported);
}

View File

@@ -31,7 +31,7 @@ public class SurfaceRenderer implements DefaultLifecycleObserver, SurfaceCallbac
private final CarContext mCarContext;
@NonNull
private final Map mMap;
private final Map mMap = new Map(Car);
@NonNull
private Rect mVisibleArea = new Rect();
@@ -45,7 +45,6 @@ public class SurfaceRenderer implements DefaultLifecycleObserver, SurfaceCallbac
{
Logger.d(TAG, "SurfaceRenderer()");
mCarContext = carContext;
mMap = new Map(Car, MwmApplication.from(mCarContext).getLocationHelper());
mIsRunning = true;
lifecycle.addObserver(this);
mMap.setMapRenderingListener(this);
@@ -60,6 +59,7 @@ public class SurfaceRenderer implements DefaultLifecycleObserver, SurfaceCallbac
mSurface.release();
mSurface = surfaceContainer.getSurface();
mMap.setLocationHelper(MwmApplication.from(mCarContext).getLocationHelper());
mMap.onSurfaceCreated(mCarContext, mSurface,
new Rect(0, 0, surfaceContainer.getWidth(), surfaceContainer.getHeight()),
surfaceContainer.getDpi());

View File

@@ -56,8 +56,8 @@ public final class Map
@NonNull
private final DisplayType mDisplayType;
@NonNull
private final LocationHelper mLocationHelper;
@Nullable
private LocationHelper mLocationHelper;
private int mCurrentCompassOffsetX;
private int mCurrentCompassOffsetY;
@@ -79,13 +79,17 @@ public final class Map
private static int sCurrentDpi = 0;
public Map(@NonNull DisplayType mapType, @NonNull LocationHelper locationHelper)
public Map(@NonNull DisplayType mapType)
{
mDisplayType = mapType;
mLocationHelper = locationHelper;
onCreate(false);
}
public void setLocationHelper(@NonNull LocationHelper locationHelper)
{
mLocationHelper = locationHelper;
}
/**
* Moves the map compass using the given offsets.
*
@@ -142,6 +146,8 @@ public final class Map
public void onSurfaceCreated(final Context context, final Surface surface, Rect surfaceFrame, int surfaceDpi)
{
assert mLocationHelper != null : "LocationHelper must be initialized before calling onSurfaceCreated";
if (isThemeChangingProcess(context))
{
Logger.d(TAG, "Theme changing process, skip 'onSurfaceCreated' callback");