2013-10-25 29 views
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); 
} 
} 

它放置在一個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

你怎麼做你的片段交易?你使用'add()'和'hide()'還是使用'replace()'? –

+0

我使用FragmentPagerAdapter,所以它基本上爲我做所有的工作。我不使用片段交易。 –

+0

不確定這是否是您想要的答案,但也許您可以移動到FragmentPagerAdapter中的片段,然後放大到咖啡店。 –

回答

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); 
} 

ActivitysetMap(map)是:

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); 
}