简体   繁体   中英

Sometimes I have a problem with google map in Android studio, not always. Why? How can I remove the white bar above google map?

这是应用程序工作的时间 这是我当应用程序不起作用时

Fragment for map:

public class DashboardFragment extends Fragment implements OnMapReadyCallback {
    GoogleMap mGoogleMap;
    MapView mMapView;
    View mView;
    private RequestQueue queue;


    public DashboardFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        queue = Volley.newRequestQueue(getActivity().getApplicationContext());

        getEarthQuakes();
    }

OnCreateView, OnViewcreated:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mView = inflater.inflate(R.layout.fragment_dashboard, container, false);
        return mView;
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mMapView = (MapView) mView.findViewById(R.id.map);
        if (mMapView != null) {
            mMapView.onCreate(null);
            mMapView.onResume();
            mMapView.getMapAsync((OnMapReadyCallback) this);

        }
    }

OnMapReady:

    @Override
    public void onMapReady(GoogleMap googleMap) {
        MapsInitializer.initialize(getContext());
        mGoogleMap = googleMap;
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        /*googleMap.addMarker(new MarkerOptions().position(new LatLng(43.84864, 18.35644)).title("Sarajevo"));
        CameraPosition Sarajevo = CameraPosition.builder().target(new LatLng(43.84864, 18.35644)).zoom(10).bearing(0).tilt(45).build();
        googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(Sarajevo));*/

    }

Sometimes the application stops, and sometimes it works okey.

    public void getEarthQuakes() {

        final Earthquake earthquake = new Earthquake();
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, Constants.URL,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray features = response.getJSONArray("features");
                    for ( int i=0; i<Constants.LIMIT;i++){
                      JSONObject properties = features.getJSONObject(i).getJSONObject("properties");
                        JSONObject geometry = features.getJSONObject(i).getJSONObject("geometry");

                        JSONArray coordinates = geometry.getJSONArray("coordinates");

                        double lon = coordinates.getDouble(0);
                        double lat = coordinates.getDouble(1);

                        Log.d("Quake", lon + ", " + lat);

When the application doesn't work this is printed in the console: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference

                        earthquake.setPlace(properties.getString("place"));
                        earthquake.setType(properties.getString("type"));
                        earthquake.setTime(properties.getLong("time"));
                        earthquake.setMagnitude(properties.getDouble("mag"));
                        earthquake.setDetailLink(properties.getString("detail"));

                        java.text.DateFormat dateFormat = java.text.DateFormat.getDateInstance();
                        Date date;
                        String formattedDate= dateFormat.format(new Date(Long.valueOf(properties.getLong("time"))));

Add markers:

                        MarkerOptions markerOptions = new MarkerOptions();
                        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
                        markerOptions.title(earthquake.getPlace());
                        markerOptions.position(new LatLng(lat,lon));
                        markerOptions.snippet("Magnitude: " +
                                earthquake.getMagnitude() + "\n" +
                                "Date: " + formattedDate);


                        Marker marker = mGoogleMap.addMarker(markerOptions);
                       // Marker marker= mGoogleMap.addMarker(markerOptions.position(new LatLng(lat,lon)));

                        marker.setTag(earthquake.getDetailLink());
                        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),1));
                    }

End of code:

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }

        });
        queue.add(jsonObjectRequest);
    }

XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.dashboard.DashboardFragment">
   <com.google.android.gms.maps.MapView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/map"
       />
</androidx.constraintlayout.widget.ConstraintLayout>

I was facing the same problem and I solved it by deleting this line: "android:paddingTop="?attr/actionBarSize" in my activity_main.xml .

Before: With action bar size

After: Without action bar size

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM