集成MapsForge库后地图显示空白,报is == null错误求助
Fixing MapsForge Blank Map with "is == null" Error
Hey there, let's work through why your MapsForge map is showing up blank— that W/DatabaseRenderer: java.lang.IllegalArgumentException: is == null error is the critical clue here. It means the .map file you're trying to load can't be found, so the input stream (is) for the MapFile is null. Let's break down the fixes step by step:
1. Fix the Map File Path (The Most Likely Culprit)
Your current code uses a relative path new File("myDirectory/myFile.map"), which doesn't work on Android. Android requires absolute paths to access files, and the path depends on where you stored your .map file:
- If the file is in external storage (like the Downloads folder), use the system's external storage directory path.
- If the file is in your app's assets folder, you can't access it directly with
File— you'll need to use anAssetFileDescriptorto get the input stream forMapFile.
2. Corrected Code with Error Checks
Here's a revised version of your code with path fixes and safeguards to catch missing files:
// First, handle storage permissions (skip if using assets) if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 100); } else { initMap(); } // Map initialization method private void initMap() { AndroidGraphicFactory.createInstance(getActivity().getApplication()); fragmentView = inflater.inflate(R.layout.maps_view, null); mMap = fragmentView.findViewById(R.id.myMap); mMap.setClickable(true); mMap.getMapScaleBar().setVisible(true); mMap.setBuiltInZoomControls(true); mMap.setCenter(new LatLong(45.48, 3)); mMap.setZoomLevel((byte)10); // Example: Get map file from external Downloads directory File mapFile = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "myFile.map" ); // Verify the file exists before loading if (!mapFile.exists()) { Log.e("MapsForge", "Map file missing at: " + mapFile.getAbsolutePath()); Toast.makeText(getActivity(), "Map file not found!", Toast.LENGTH_SHORT).show(); return; } mTileCache = AndroidUtil.createTileCache( getActivity(), "mapcache", mMap.getModel().displayModel.getTileSize(), 1f, mMap.getModel().frameBufferModel.getOverdrawFactor() ); mapsData = new MapFile(mapFile); mTileRenderer = new TileRendererLayer( mTileCache, mapsData, mMap.getModel().mapViewPosition, AndroidGraphicFactory.INSTANCE ); mTileRenderer.setXmlRenderTheme(InternalRenderTheme.DEFAULT); // Fix the typo: use mMap instead of mMapo! mMap.getLayerManager().getLayers().add(mTileRenderer); mMap.setCenter(new LatLong(45.48, 3.05)); mMap.setZoomLevel((byte) 12); }
3. Additional Checks to Rule Out Other Issues
- Validate the .map file: Open it with a desktop MapsForge viewer (like MapsForge Desktop) to confirm it's not corrupted.
- Check for typos: Your original code had
mMapo.getLayerManager()— that's probably a typo formMap, which would prevent the tile layer from being added to the map. - Confirm dependency consistency: You're using 0.9.1 across all MapsForge dependencies, which is good— mismatched versions can cause unexpected bugs.
内容的提问来源于stack exchange,提问作者Zelig
相关产品推荐
相关产品推荐

