gpt4 book ai didi

android - 在 android 中的 TabHost + fragment 中调用 oncreateView 时出错?

转载 作者:行者123 更新时间:2023-11-29 17:51:06 24 4
gpt4 key购买 nike

我正在开发一个有 4 个标签的应用程序,其中 1 个标签是谷歌地图。

当用户点击它时,它可以正确地创建 map ,但是,如果用户通过点击其他标签离开当前标签,然后再次回到这个标签,它会抛出 error inflating layout XML 的异常。

所以我记录流程,我发现在第二次的onCreateView中,已经创建了rootview,但是当我添加if (rootView == null)时,它仍然抛出“需要”的错误在 subview 上删除 View ”。如何解决?谢谢。

public class Map extends Fragment {
// Google Map
private GoogleMap googleMap;
private boolean isShowRoute;
private Database db;
private ArrayList<Restaurant> rList;
private ArrayList<Spot> sList;

View rootView;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("test1","" + rootView);
rootView = inflater.inflate(R.layout.map, container, false);
return rootView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.d("test1","b");
super.onActivityCreated(savedInstanceState);
db = new Database (getActivity());
rList = db.getRestaurant();
sList = db.getSpot();
try {
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}

}

/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();

// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity().getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
} else {
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);

if (isShowRoute)
showRoute();
else
pinMap();
}
}
}


private void showRoute() {
// if (RunTracker.routePoints.size() > 1){
// Polyline route = googleMap.addPolyline(new PolylineOptions()
// .geodesic(true));
// route.setPoints(RunTracker.routePoints);
//
// LatLng lastLatLng = RunTracker.routePoints.get(RunTracker.routePoints.size() - 1);
// googleMap.addMarker(new MarkerOptions().position(lastLatLng).title("You are here"));
// googleMap.moveCamera( CameraUpdateFactory.newLatLngZoom(lastLatLng, 16.0f) );
// }
}

private void pinMap() {
for (Restaurant r : rList) {
MarkerOptions marker = new MarkerOptions().position(new LatLng(r.lat, r.lng)).title(r.name);
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
googleMap.addMarker(marker);
}

for (Spot s : sList) {
MarkerOptions marker = new MarkerOptions().position(new LatLng(s.lat, s.lng)).title(s.name);
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
googleMap.addMarker(marker);
}

googleMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(22.3079136,114.1778376), 10.0f) ); // go to MK MTR station by default
}

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

}

日志猫

03-31 14:53:56.669: E/AndroidRuntime(4611): android.view.InflateException: Binary XML file line #6: Error inflating class fragment

Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f050006, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment

布局

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

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>

更新代码:

   @Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
db = new Database (getActivity());

if (rList == null && sList == null) {
rList = db.getRestaurant();
sList = db.getSpot();
}
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

rootView = inflater.inflate(R.layout.map, container, false);

try {
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}

return rootView;
}

最佳答案

@Override
public void onDestroyView() {
super.onDestroyView();
if (mMap != null) {
getActivity().getSupportFragmentManager().beginTransaction().remove(getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).commit();
mMap = null;
}
}

必须从 FragmentManager 中删除 mapfragment 的 id否则,如果相同,则下一次传递应用程序会崩溃

关于android - 在 android 中的 TabHost + fragment 中调用 oncreateView 时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22754150/

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