gpt4 book ai didi

android - PagerAdapter 中的 MapFragment

转载 作者:行者123 更新时间:2023-11-30 01:16:58 26 4
gpt4 key购买 nike

我正在尝试在 PagerAdapter 中加载 Mapfragment。这是适配器代码,

public class ShopPagerAdapter extends PagerAdapter {

private List<LoyaltyOutlet> data;
private Context context;
FragmentManager fragmentManager;

public ShopPagerAdapter(List<LoyaltyOutlet> data, Context context,FragmentManager manager) {
this.data = data;
this.context = context;
this.fragmentManager = manager;
}

@Override
public Object instantiateItem(ViewGroup collection, int position) {
LoyaltyOutlet outlet = data.get(position);
LayoutInflater inflater = LayoutInflater.from(context);
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.fragment_loyalty_shop_item, collection, false);
TextView address = (TextView)layout.findViewById(R.id.txt_ShopAddress);
GoogleMap googleMap = ((SupportMapFragment) fragmentManager.findFragmentById(R.id.shopMap)).getMap();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
googleMap.animateCamera(cameraUpdate);
address.setText(outlet.getOutletName());

collection.addView(layout);
return layout;

}

@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}

@Override
public int getCount() {
return data.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}

@Override
public CharSequence getPageTitle(int position) {
return "";
}

}

但是当我运行代码时出现异常,

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference

是否可以在这里使用 MapFragment,或者我应该将这段代码移到 fragment 生命周期方法中吗?

谢谢。

最佳答案

这就是我使用 MapView 实现它的方式。希望这会有所帮助

fragment 的Xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>

Java 代码

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_lugar,container,false);
latitude = 26.78;
longitude = 72.56;
mMapView = (MapView) view.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);

mMapView.onResume();// needed to get the map to display immediately

try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}

googleMap = mMapView.getMap();
// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;

// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");

// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(17.385044, 78.486671)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
return view;
}
/***** Sets up the map if it is possible to do so *****/
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}

@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}

@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}

@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}

关于android - PagerAdapter 中的 MapFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37703090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com