gpt4 book ai didi

onCreateView() 中抽屉导航中的 Android Google Map v2

转载 作者:行者123 更新时间:2023-11-30 02:44:28 25 4
gpt4 key购买 nike

我的 Android 应用程序有问题。我在 PlaceholderFragment 中使用带 onCreateView() 的抽屉导航,如下所示:

// Google Map
private GoogleMap map;
private final LatLng LOCATION_DEPAUW = new LatLng(39.640343, -86.860687);

public static class PlaceholderFragment extends Fragment {

/**
* Returns a new instance of this fragment for the given section number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}

public PlaceholderFragment() {
}

@SuppressWarnings("unused")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = null;

...

else if(mTitle.equals("Map")){
Log.d("Map", "Worked");
rootView = inflater.inflate(R.layout.google_map, container, false);

try {

if (map == null) {
SupportMapFragment fm = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
map = fm.getMap();
}


// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}

CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_DEPAUW, 15);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Enabling MyLocation Layer of Google Map
map.setMyLocationEnabled(true);
map.getUiSettings().setZoomGesturesEnabled(true);
map.getUiSettings().setCompassEnabled(true);
map.getUiSettings().setRotateGesturesEnabled(true);
map.animateCamera(update);

/*
* for different colors:
* googlemap.addMarker(new MarkerOptions().position(new LatLng( 65.07213,-2.109375)).title("This is my title").snippet("and snippet").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
*
* .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE) is the key. change the HUE_ORANGE to anything that is available
*
*/

} catch (Exception e) {
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
}
}

我已经正确导入了所有内容,并且 map 在第一个 View 中正确初始化。问题是当我导航到另一个 fragment 然后返回到这个 map fragment 时,我的应用程序崩溃了。我试过使用 eclipse 模拟器,但无法正确配置它。有谁知道在 placeholderfragment 中处理 map 生命周期的方法?谢谢!

最佳答案

经过大量研究,我发现有人设法很好地处理了这个问题。

查看这个 github 存储库:

https://gist.github.com/joshdholtz/4522551

map View map View ; 谷歌地图;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.some_layout, container, false);

// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);

// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);

// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}

// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
map.animateCamera(cameraUpdate);

return v;
}

@Override
public void onResume() {
mapView.onResume();
super.onResume();
}

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

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

但最初,我遇到了一些问题,因为我有多个 fragment 可以在它们之间切换,所以我在我的 fragment 中这样做了:

public void onResume() {        

try{
super.onResume();
mapView.onResume();
}catch(NullPointerException e){
Log.d("onResume", "NullPointerException: " + e);
}
}

@Override
public void onDestroy() {
try{
super.onDestroy();
mapView.onDestroy();
}catch(NullPointerException e){
Log.d("onDestroy", "NullPointerException: " + e);
}
}

@Override
public void onLowMemory() {
try{
super.onLowMemory();
mapView.onLowMemory();
}catch(NullPointerException e){
Log.d("onLowMemory", "NullPointerException: " + e);
}
}

关于onCreateView() 中抽屉导航中的 Android Google Map v2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25316498/

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