重新打开Goog​​le地图会停止我的应用程序

时间:2017-07-31 14:09:11

标签: google-maps android-fragments

我有两个片段。点击第一个片段按钮,谷歌地图的第二个片段将会打开。

第一次所有事情都是正确的,但如果我回来再次点击,程序将会停止。

地图片段代码就像打击一样:

public class map extends Fragment implements OnMapReadyCallback {
    private GoogleMap mMap;
    private double lat;
    private double att;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.map,container,false);

       // GoogleMap map = ((MapFragment) getFragmentManager(.findFragmentById(R.id.map)).getMap();

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

        if (getArguments() != null) {
            lat= getArguments().getDouble("LAT");
            att=getArguments().getDouble("ATT");
            Toast.makeText(getActivity(),String.valueOf(att),Toast.LENGTH_LONG).show();
            MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
        }
    }

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

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }
    @Override
    public void onMapReady(GoogleMap googleMap) {


        // Add a marker in Sydney and move the camera
         LatLng sydney = new LatLng(lat, att);
        CameraPosition googlePlex = CameraPosition.builder()
                .target(new LatLng(lat,att))
                .zoom(16).build();
        googleMap.addMarker(new MarkerOptions().position(sydney));
        googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(googlePlex));

    }
}

主要活动是:

public class MainActivity extends AppCompatActivity implements onclicklistener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(findViewById(R.id.firstpage)!=null)
        {
            if(savedInstanceState!=null)
                return;
            firstpage startpage=new firstpage();
            FragmentManager fm=getFragmentManager();
            FragmentTransaction ft=fm.beginTransaction();


            ft.add(R.id.firstpage,startpage);
            ft.commit();


        }
    }
@Override
    public void onclickbutton(String index) {
    try{ map newmap=new map();
             Bundle args = new Bundle();
             args.putDouble("LAT",32.657892);
            args.putDouble("ATT",51.668643);
        FragmentManager fm=getFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
       newmap.setArguments(args);
         ft.addToBackStack(null);
            ft.replace(R.id.firstpage,newmap);

            ft.commit();}
            catch (Exception e){Toast toast = Toast.makeText(this, "error", Toast.LENGTH_SHORT);}
        }

    @Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() > 0 ){
            getFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
    }

    }

首次运行完全正常,但如果我再次点击,程序将停止。请指导我。

1 个答案:

答案 0 :(得分:0)

它解决了。

我添加了这段代码:

 @Override
    public void onDetach() {
        Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
        FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
        super.onDetach();
    }