我有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);
}
}
它被放置在ViewPager
并且效果很好,直到我拨打zoomToSelectedCoffeeShop
方法。我在这一行得到NullPointerException
:
map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
我无法弄明白为什么。此方法是从Fragmnet
(另一个Fragment
或活动)外部调用的。我如何获得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
}
答案 0 :(得分: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)
的{{1}}为:
Activity
然后在活动中我可以操纵地图:
public void setMap(GoogleMap map)
{
this.map = map;
}