0

我有这个Fragment

public class MyMapFragment extends Fragment {

private static final LatLng SOME_PLACE = new LatLng(32.073229,34.773231);
private GoogleMap map;
ArrayList<CoffeeShop> coffeeShopsList;

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.map_fragment, null, false);   
    map =  ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
    map.setMyLocationEnabled(true);

    coffeeShopsList = (ArrayList<CoffeeShop>) ((CupsDemoApplication)getActivity().getApplication()).getCoffeeShopsList();

    for (CoffeeShop tempCoffeeShop: coffeeShopsList)
    {
         Marker marker = map.addMarker(new MarkerOptions()
        .position(new LatLng(Double.parseDouble(tempCoffeeShop.getCoffeeShopLat()), Double.parseDouble(tempCoffeeShop.getCoffeeShopLng())))
        .title(tempCoffeeShop.getCoffeeShopName())
        .snippet(tempCoffeeShop.getCoffeeShopAddress())
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.pinplace_turkiz)));
    }       

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(SOME_PLACE, 2));
    map.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);

    return v;
}

public void zoomToSelectedCoffeeShop(LatLng latlng)
{
    Log.d("EMIL", "latlng: "+latlng );
    if (map == null)
    {
        map =  ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
    }
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 2));
    map.animateCamera(CameraUpdateFactory.zoomTo(17), 2000, null);  
}
 }

它被放置在 a 中ViewPager并且运行良好,直到我调用该zoomToSelectedCoffeeShop 方法。我NullPointerException在这条线上得到一个:

map =  ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

我不知道为什么。此方法是从Fragmnet(另一个Fragment或 Activity)外部调用的。我将如何获得SupportMapFragment操纵它的实例?

这是FragmentPagerAdapter

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment;
        Bundle args = new Bundle();
        args.putInt(ShopsListFragment.ARG_QUESTION_NUMBER, position);
        switch (position) {
        case 0:
            fragment = new ShopsListFragment();
            fragment.setArguments(args);
            return fragment;
        case 1:
            fragment = new MyMapFragment();
            fragment.setArguments(args);
            return fragment;
        }
        return null;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.coffee_shops_section).toUpperCase(l);
        case 1:
            return getString(R.string.map_section).toUpperCase(l);
        }
        return null;
    }
}

@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
}
4

1 回答 1

0

为了解决这个问题并从活动中操作地图,我将地图实例传递给活动,如下所示:

@Override
public void onResume() {
    super.onResume();
    if (map == null) {
        map = fragment.getMap();
    }
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);   
    this.activity = (MainActivity) activity;
}

@Override
public void onStart() {
    super.onStart();
    activity.setMap(map);
}

什么时候setMap(map)Activity

public void setMap(GoogleMap map)
{
    this.map = map;
} 

然后在活动中我可以操纵地图:

@Override
public void onCoffeeShopSelected(LatLng latlng) 
{
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 14));
    map.animateCamera(CameraUpdateFactory.zoomTo(17), 2000, null);  
}
于 2013-10-25T23:28:44.147 回答