Android内部片段映射

时间:2014-11-30 16:41:09

标签: android google-maps android-fragments

我的应用程序使用NavigationDrawerFragment。通过抽屉中的每个菜单元素,我需要包含一个Activity。问题是在setUpMapIfNeeded()中我总是将googleMap视为null。这是我的代码:

public class AppActivity extends FragmentActivity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks, MapFragment.OnFragmentInteractionListener {

private static final String TAG = "APP_ACTIVITY";
private NavigationDrawerFragment mNavigationDrawerFragment;
private CharSequence mTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    switch (position){
        case 0:
            fragmentManager
                    .beginTransaction()
                    .replace(R.id.container, MapFragment.newInstance(position))
                    .commit();
            break;
    }
}

public void onSectionAttached(int number) {
    switch (number) {
        case 1:
            mTitle = getString(R.string.title_section1);
            break;
        case 2:
            mTitle = getString(R.string.title_section2);
            break;
        case 3:
            mTitle = getString(R.string.title_section3);
            break;
    }
}

public void restoreActionBar() {
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        getMenuInflater().inflate(R.menu.app, menu);
        restoreActionBar();
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onFragmentInteraction(Uri uri) {
    Log.v(TAG, "onFragmentInteraction: " + uri);
}

}

使用此xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:layout_gravity="right" />

</LinearLayout>

嗯,fragmentActivity已实例化,但fragmentActivity看起来像:

public class MapFragment extends AbstractAppFragment实现了GoogleMap.OnCameraChangeListener {

protected static final String ARG_POSITION = "ARG_POSITION";
private Location myLocation;
private MapView mapView;
public GoogleMap googleMap;
private float zoom = 15.0f;
private float previousZoomLevel = -1.0f;
private boolean isZooming = false;

private OnFragmentInteractionListener mListener;

public static MapFragment newInstance(int position) {
    MapFragment mapFragment = new MapFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_POSITION, position);
    mapFragment.setArguments(args);
    return mapFragment;
}

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

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.v(TAG, "ON_CREATE");
    super.onCreate(savedInstanceState);

    setUpMapIfNeeded();
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.v(TAG, "onActivityCreated: ");
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.v(TAG, "onCreateView: ");
    View view = inflater.inflate(R.layout.activity_map, container, false);
    return view;
}

@Override
public void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

private void setUpMapIfNeeded() {
    if (googleMap == null) {
        Log.v(TAG, "WTF: " + (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map));
        //googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        Log.v(TAG, "setUpMapIfNeeded: " + googleMap);
        if (googleMap != null) {
            googleMap.setOnCameraChangeListener(getCameraChangeListener());
            //getLocation();
            //setUpMap();
        }
    }
}



/*@Override
public void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}




public GoogleMap.OnCameraChangeListener getCameraChangeListener(){
    return new GoogleMap.OnCameraChangeListener(){
        @Override
        public void onCameraChange(CameraPosition position){
            Log.d("Zoom", "Zoom: " + position.zoom);
            if(previousZoomLevel != position.zoom){
                isZooming = true;
            }
            previousZoomLevel = position.zoom <= 2.0 ? zoom : position.zoom;
        }
    };
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    public void onFragmentInteraction(Uri uri);
}

}

1 个答案:

答案 0 :(得分:0)

所以试试吧。将setUpMapIfNeeded()放在onCreate方法中。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setUpMapIfNeeded();

另外,尝试在片段中进行空检查。

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();

希望它有所帮助。