代码之家  ›  专栏  ›  技术社区  ›  Mazharul Islam

无法获取GoogleMap对象

  •  1
  • Mazharul Islam  · 技术社区  · 9 年前

    我已经创建了MapFragment类。在这里,我创建了GoogleMap对象 googleMap = mMapView.getMap() 我有一个方法 分析器任务 如果我打电话 分析器任务 然后从该方法的onCreate 分析器任务 工作成功 googleMap.addPolyline(lineOptions) 作品但如果我调用方法 分析器任务 然后从另一个活动 googleMap.addPolyline(线条选项) 不起作用,它显示空指针异常。我想在这里 谷歌地图 无法获取MapView的实例。现在谁能给我一个建议吗?这样我就可以得到 谷歌地图 在里面 分析器任务 如果它是从另一个活动调用的。我在下面附上了我的代码:

    public class MapFragment extends Fragment implements
            GoogleMap.OnInfoWindowClickListener{
    
        MapView mMapView;
    
        private static final int MAP_ZOOM_AMOUNT=17;
        private TextView locName;
        private TextView mapIndicator;
        private GoogleMap googleMap=null;
        private String locationName;
        private String mapIndicatorText;
        private int categoryId;
        private int locationNameId;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_map, container,
                    false);
            mMapView = (MapView) rootView.findViewById(R.id.mapView);
            locName = (TextView) rootView.findViewById(R.id.tv_location_name);
            mapIndicator = (TextView) rootView.findViewById(R.id.tv_map_indicator);
            locName.setText(locationName);
            mapIndicator.setText(Html.fromHtml("সব " + mapIndicatorText + " এর স্থান ম্যাপ এ দেখানো হয়েছে"));
            mMapView.onCreate(savedInstanceState);
            mMapView.onResume();
    
            try {
                MapsInitializer.initialize(getActivity().getApplicationContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            googleMap = mMapView.getMap();
            googleMap.setOnInfoWindowClickListener(this);
    
            return rootView;
        }
        @Override
        public void onInfoWindowClick(Marker marker) {
            LatLng loc = marker.getPosition();
    
        }
    
    
        private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String,String>>> >{
    
            // Parsing the data in non-ui thread
            @Override
            protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {
    
                JSONObject jObject;
                List<List<HashMap<String, String>>> routes = null;
    
                try{
                    jObject = new JSONObject(jsonData[0]);
                    RouteActivity parser = new RouteActivity();
    
                    // Starts parsing data
                    routes = parser.parse(jObject);
                }catch(Exception e){
                    e.printStackTrace();
                }
                return routes;
            }
    
            // Executes in UI thread, after the parsing process
            @Override
            protected void onPostExecute(List<List<HashMap<String, String>>> result) {
    
                ArrayList<LatLng> points = null;
                PolylineOptions lineOptions = null;
    
                // Traversing through all the routes
                for(int i=0;i<result.size();i++){
                    points = new ArrayList<LatLng>();
                    lineOptions = new PolylineOptions();
    
                    // Fetching i-th route
                    List<HashMap<String, String>> path = result.get(i);
    
                    // Fetching all the points in i-th route
                    for(int j=0;j<path.size();j++){
                        HashMap<String,String> point = path.get(j);
    
                        double lat = Double.parseDouble(point.get("lat"));
                        double lng = Double.parseDouble(point.get("lng"));
                        LatLng position = new LatLng(lat, lng);
    
                        points.add(position);
                    }
    
                    // Adding all the points in the route to LineOptions
                    lineOptions.addAll(points);
                    lineOptions.width(2);
                    lineOptions.color(Color.RED);
                }
                //setUpMapIfNeeded();
                // Drawing polyline in the Google Map for the i-th route
                ***googleMap.addPolyline(lineOptions);***
    
            }
        }
    
    
    
        @Override
        public void onResume() {
            super.onResume();
            mMapView.onResume();
        }
    
        @Override
        public void onPause() {
            super.onPause();
            mMapView.onPause();
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            mMapView.onDestroy();
        }
    
        @Override
        public void onLowMemory() {
            super.onLowMemory();
            mMapView.onLowMemory();
        }
    
    }
    
    2 回复  |  直到 9 年前
        1
  •  1
  •   RogueBaneling    9 年前

    您在评论中表示您正在运行 ParserTask 如下:

    MapFragment mapFragment = new MapFragment();
    mapFragment.parserTask(result);
    

    问题是 MapView MapFragment onCreateView 运行,当 Fragment 进入视野。所以 地图视图 当您尝试运行 分析器任务 。您可以选择重新分级您的类结构(可能是更好的解决方案,但也需要更多的工作),或者您可以尝试以下操作:

    首先达到班级水平 PolylineOptions polylineOptions; 变量在您的 onPostExecute() 代替 googleMap.addPolyline(lineOptions); 具有

    if (googleMap != null) {
        googleMap.addPolyline(lineOptions);
    } else {
        polylineOptions = lineOptions;
    }
    

    在您的 创建视图时 ,你可以写:

    if (polylineOptions != null) {
        googleMap.addPolyline(polylineOptions);
    }
    

    其思想是,如果异步任务在视图渲染之前完成,则在视图渲染时保存数据,如果视图在异步任务完成之前渲染,则立即应用效果。

        2
  •  1
  •   Octavian Ionel    9 年前

    我没有使用AsyncTask。相反,我使用了一个处理程序和一个线程。

    您可以删除AsyncTask的部分,然后尝试我的代码。

        Handler mainHandler = new Handler(Looper.getMainLooper());
    
        Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                mapView.getMapAsync(new OnMapReadyCallback() {
                    @Override
                    public void onMapReady(GoogleMap googleMap) {
                        googleMap.addMarker(marker1);
                        googleMap.addMarker(marker2);
                        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng1, zoom));
                        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng2, zoom));
                        polylineOptions.addAll(points);
                        polylineOptions.width(10);
                        polylineOptions.color(Color.BLUE);
                        googleMap.addPolyline(polylineOptions);
    
                        //disable the rotation, scroll of the map
                        googleMap.getUiSettings().setAllGesturesEnabled(false);
    
                    }
                });
            }
        };
    
        mainHandler.post(myRunnable);