Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓谷歌地图在片段导航后崩溃_Android_Android Fragments_Google Maps Android Api 2_Android Maps V2_Android Navigation - Fatal编程技术网

Android 安卓谷歌地图在片段导航后崩溃

Android 安卓谷歌地图在片段导航后崩溃,android,android-fragments,google-maps-android-api-2,android-maps-v2,android-navigation,Android,Android Fragments,Google Maps Android Api 2,Android Maps V2,Android Navigation,我有一个带有导航抽屉的应用程序,在用户选择导航项目后,使用fragment显示新屏幕 我的MainActivity类中处理以下内容的部分: public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.nav_new) {

我有一个带有导航抽屉的应用程序,在用户选择导航项目后,使用fragment显示新屏幕

我的MainActivity类中处理以下内容的部分:

public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_new) {
            addFragment(new NewFragment(), id);
        } else if (id == R.id.nav_start) {
            addFragment(new StartFragment(), id);
        } else if (id == R.id.nav_delete) {
            addFragment(new DeleteFragment(), id);
        } else if (id == R.id.nav_gps) {
            addFragment(new GPSFragment(), id);
        }
...
public void addFragment(Fragment fragment, int id) {
        if (fragment != null) {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            if (fragmentManager.getFragments() == null) {
                fragmentTransaction.replace(R.id.fragment_container, fragment, fragment.getClass().getSimpleName())
                        .commit();
            } else {
                fragmentTransaction.replace(R.id.fragment_container, fragment, fragment.getClass().getSimpleName())
                        .addToBackStack(Integer.toString(id))
                        .commit();
            }
        }
    }
addFragment
方法如下所示:

public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_new) {
            addFragment(new NewFragment(), id);
        } else if (id == R.id.nav_start) {
            addFragment(new StartFragment(), id);
        } else if (id == R.id.nav_delete) {
            addFragment(new DeleteFragment(), id);
        } else if (id == R.id.nav_gps) {
            addFragment(new GPSFragment(), id);
        }
...
public void addFragment(Fragment fragment, int id) {
        if (fragment != null) {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            if (fragmentManager.getFragments() == null) {
                fragmentTransaction.replace(R.id.fragment_container, fragment, fragment.getClass().getSimpleName())
                        .commit();
            } else {
                fragmentTransaction.replace(R.id.fragment_container, fragment, fragment.getClass().getSimpleName())
                        .addToBackStack(Integer.toString(id))
                        .commit();
            }
        }
    }
其中一个片段是GPS,我在其中实现了谷歌地图。这是可行的,但有两个主要问题,我不确定发生了什么:

  • 当我使用导航抽屉导航到GPS,然后导航到另一个片段,然后导航到GPS(重复多次),最终屏幕将变黑并完全重新加载主活动
  • 当我使用后退按钮(从GPS导航到另一个片段,返回GPS,重复多次)或使用home按钮暂停应用程序并使其返回到resume(重复多次)时,应用程序会变得非常缓慢,需要大约一分钟来响应任何用户输入
  • 这一定是因为我实现谷歌地图的方式,或者更具体地说是我处理不同州之间转换的方式(可能)。我想,按照我实现代码的方式,每当我使用导航抽屉,或按下后退按钮,或按home离开GPS碎片时,GMAP就会断开连接,碎片就会被挂起

    我不知道为什么这会导致如此缓慢的速度,或者重新启动应用程序回到主活动。也许是记忆问题?我不确定如果我总是在离开的时候从GMAP上断开连接并移除碎片,那会是什么原因造成的。在我添加代码以使
    mMapView.getmapsync(this)
    正常工作之前,我认为这不是一个问题,因此它可能与mapready事件有关,以及它如何与导航交互

    GPSfragment只需监听lat、lon位置的变化,并将相机和标记器移动到用户位置的中心。以下是GPSFragment类:

    public class GPSFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener, LocationListener, OnMapReadyCallback {
    
        private GoogleMap googleMap;
        private GoogleApiClient mGoogleApiClient;
        double lat, lon;
        LocationRequest mLocationRequest;
        CoordinatorLayout coordinatorLayout;
        UiSettings mapSettings;
        CameraPosition cameraPosition;
        MarkerOptions markerOptions;
        Marker marker;
        Location mLastLocation;
        MapView mMapView;
    
        public GPSFragment() {
            // Required empty public constructor
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            coordinatorLayout = (CoordinatorLayout) getActivity().findViewById(R.id.coordinator_layout);
    
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_gps, container, false);
    
            //Set the nav drawer item highlight
            MainActivity mainActivity = (MainActivity) getActivity();
            mainActivity.navigationView.setCheckedItem(R.id.nav_gps);
    
            //Set actionbar title
            mainActivity.setTitle("GPS");
    
            //Create api connector and map
            buildGoogleApiClient();
    
            mMapView = (MapView) view.findViewById(R.id.mapView);
            mMapView.onCreate(savedInstanceState);
            //mMapView.onResume();
    
            //When map is ready, set it up with a onMapReady callback
            mMapView.getMapAsync(this);
    
            return view;
        }
    
        @Override
        public void onResume() {
            super.onResume();
    
            if (mGoogleApiClient.isConnected()) {
                mMapView.onResume();
            } else {
                mGoogleApiClient.connect();
                mMapView.onResume();
            }
    
        }
    
        @Override
        public void onPause() {
            super.onPause();
    
            if (mGoogleApiClient.isConnected()) {
                mMapView.onPause();
                mGoogleApiClient.disconnect();
            }
    
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
    
            if (mGoogleApiClient.isConnected()) {
                mMapView.onDestroy();
                mGoogleApiClient.disconnect();
            }
    
        }
    
        @Override
        public void onLowMemory() {
            super.onLowMemory();
    
            if (mGoogleApiClient.isConnected()) {
                mMapView.onLowMemory();
                mGoogleApiClient.disconnect();
            }
    
        }
    
        @Override
        public void onConnected(Bundle bundle) {
            mLocationRequest = LocationRequest.create();
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            mLocationRequest.setInterval(100); // Update location every second
    
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    
            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            if (mLastLocation != null) {
                lat = mLastLocation.getLatitude();
                lon = mLastLocation.getLongitude();
            }
        }
    
        @Override
        public void onConnectionSuspended(int i) {
    
        }
    
        @Override
        public void onLocationChanged(Location location) {
            lat = location.getLatitude();
            lon = location.getLongitude();
    
            marker.setPosition(new LatLng(lat, lon));
    
            googleMap.animateCamera(CameraUpdateFactory
                    .newLatLng(new LatLng(lat, lon)));
        }
    
        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            buildGoogleApiClient();
        }
    
        synchronized void buildGoogleApiClient() {
            //Request permission for location data
            if (ContextCompat.checkSelfPermission(getActivity(),
                    Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
    
                //If permission has not been granted
                if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                        Manifest.permission.ACCESS_FINE_LOCATION)) {
                    //If they have previously declined permission, show explanation
                    Snackbar.make(coordinatorLayout, R.string.permission_explain_gps,
                            Snackbar.LENGTH_INDEFINITE).setAction(R.string.request_permission, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ActivityCompat.requestPermissions(getActivity(),
                                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                    0);
                        }
                    }).show();
                } else {
                    //If no previous declines
                    ActivityCompat.requestPermissions(getActivity(),
                            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                            0);
                }
            } else {
                //If permission has been granted
                mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this)
                        .addApi(LocationServices.API)
                        .build();
                mGoogleApiClient.connect();
            }
    
        }
    
        @Override
        public void onMapReady(GoogleMap mMapView) {
            googleMap = mMapView;
            setupMap();
        }
    
        public void setupMap() {
            mapSettings = googleMap.getUiSettings();
            mapSettings.setZoomGesturesEnabled(true);
            mapSettings.setZoomControlsEnabled(true);
            mapSettings.setScrollGesturesEnabled(true);
            mapSettings.setTiltGesturesEnabled(true);
            mapSettings.setRotateGesturesEnabled(true);
    
            markerOptions = new MarkerOptions().position(new LatLng(lat, lon));
            marker = googleMap.addMarker(markerOptions);
    
            cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(lat, lon)).zoom(12).build();
            googleMap.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraPosition));
    
        }
    }
    
    编辑:还刚刚注意到,在关闭并从手机上卸载应用程序后,地图标记图标仍然位于顶部的状态栏中,我认为只有当手机当前请求位置信息时,该图标才会亮起