我有这个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
}