无法在FragmentActivity

时间:2015-05-12 19:42:57

标签: android google-maps android-fragments dictionary fragment

嗨,大家好,如果我做错了,请对不起

所以我想显示一张地图,我的代码完全可以单独使用,但是当我在我的应用片段中复制/浏览它时,地图就不会出现了!

这是我的活动代码:

package com.example.lokman.tryouts;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.List;

public class FragmentTab3 extends FragmentActivity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener
{
    private static final int GPS_ERRORDIALOG_REQUEST = 9001;
    GoogleMap mMap;
    @SuppressWarnings("unused")
    private static final double


    GoogleApiClient googleApiClient;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Parse.enableLocalDatastore(this);
        Parse.initialize(this, "gj36VxUgttoM2BxT0YF65ABEIXxKJn8a6Synb3Bj", "UoVZa1wOUYFGvMwDPyyrDbj0pGEKYGvdz2dmrmiB");

        if (servicesOk()) {
            setContentView(R.layout.fragmenttab3);
            ParseQuery<ParseObject> query = ParseQuery.getQuery("Lieu");
            query.findInBackground(new FindCallback<ParseObject>() {
                @Override
                public void done(List<ParseObject> arg0, com.parse.ParseException e) {
                    if (e == null) {
                        for (ParseObject item : arg0) {
                            String title = item.getString("name");
                            Double lat = (Double) item.getNumber("latitude");
                            Double lng = (Double) item.getNumber("longitude");
                            MarkerOptions marker = new MarkerOptions()
                                    .position(new LatLng(lat, lng))
                                    .title(""+title);
                            mMap.addMarker(marker);

                        }
                    }

                }
            });

            if(initMap()){
                mMap.setMyLocationEnabled(true);

                googleApiClient = new GoogleApiClient.Builder(this)
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this)
                        .addApi(LocationServices.API)
                        .build();
                googleApiClient.connect();

            }
            else{
                Toast.makeText(this, "Map Is Not Avaible !", Toast.LENGTH_SHORT).show();

            }


        } else {
            setContentView(R.layout.activity_main);

        }

    }

    public boolean servicesOk(){
        int isAvaible = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (isAvaible == ConnectionResult.SUCCESS) {
            return true;
        }
        else if (GooglePlayServicesUtil.isUserRecoverableError(isAvaible)) {

            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvaible, this, GPS_ERRORDIALOG_REQUEST);
            dialog.show();
        }
        else {
            Toast.makeText(this,"Can't Connect to google play services !",Toast.LENGTH_SHORT).show();
        }
        return false;
    }

    private boolean initMap(){
        if (mMap == null){
            SupportMapFragment mapFragment =
                    (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            mMap = mapFragment.getMap();
        }
        return(mMap != null);


    }





    @Override
    public void onConnected(Bundle bundle) {
        Toast.makeText(this,"Connected TO Location Service !",Toast.LENGTH_SHORT).show();


    }

    @Override
    public void onConnectionSuspended(int i) {

    }


    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }



}

1 个答案:

答案 0 :(得分:0)

有类似的问题。你可以试试这个。将getSupportFragmentManager更改为GetChildFragmentManager

private boolean initMap(){
    if (mMap == null){
        SupportMapFragment mapFragment =
                (SupportMapFragment) getActivity().getChildFragmentManager().findFragmentById(R.id.map);
        mMap = mapFragment.getMap();
    }
    return(mMap != null);


}