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
}
4

1 に答える 1