有时我在 Android Studio 中遇到谷歌地图问题,但并非总是如此。为啥?如何删除谷歌地图上方的白条?

Posted

技术标签:

【中文标题】有时我在 Android Studio 中遇到谷歌地图问题,但并非总是如此。为啥?如何删除谷歌地图上方的白条?【英文标题】:Sometimes I have a problem with google map in Android studio, not always. Why? How can I remove the white bar above google map?有时我在 Android Studio 中遇到谷歌地图问题,但并非总是如此。为什么?如何删除谷歌地图上方的白条? 【发布时间】:2020-08-25 18:08:20 【问题描述】:

地图片段:

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));*/

    

应用程序有时会停止,但有时会正常运行。

    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);

当应用程序不工作时,会在控制台中打印: 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)' 在空对象引用上

                        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"))));

添加标记:

                        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));
                    

代码结束:

                 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_
    android:layout_
    tools:context=".ui.dashboard.DashboardFragment">
   <com.google.android.gms.maps.MapView
       android:layout_
       android:layout_
       android:id="@+id/map"
       />
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

你好@Anida你能证明异常触发的代码行吗?哪一行是 134 和 99?还提供片段的布局xml,这样我们就可以看到为什么你的片段中有这个差距 在这一行:Marker marker = mGoogleMap.addMarker(markerOptions) 您也可以发布您的Fragment 布局吗? 好吧,我发了。 【参考方案1】:

我遇到了同样的问题,我通过在我的activity_main.xml 中删除这一行来解决它:"android:paddingTop="?attr/actionBarSize"

之前: With action bar size

之后: Without action bar size

【讨论】:

以上是关于有时我在 Android Studio 中遇到谷歌地图问题,但并非总是如此。为啥?如何删除谷歌地图上方的白条?的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio、谷歌 Firebase、谷歌地图

我在android studio中遇到过这些错误?

我在构建应用程序时在 android studio 中遇到了 Android 资源链接失败错误。我不知道如何解决这个问题。我是 Android Studio 的新手 [关闭]

我在android studio配置中遇到问题可以帮助我吗?

我在 android studio 中遇到了这个错误 Error:java.util.concurrent.ExecutionException: com.android.builder.intern

我在 android studio 中的位置有问题