gpt4 book ai didi

第二次充气时出现 Android map v2 错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:52:44 24 4
gpt4 key购买 nike

我正在尝试在我的应用中使用新的 Android map 。

我有一个 FragmentActivity 布局包含(除其他外):

<LinearLayout a:id="@+id/fragment_container"
a:layout_width="fill_parent"
a:layout_height="100dp"
a:layout_weight="1"/>

它还有一个按钮,可以使用(主要从示例项目复制)更改此 fragment :

if (condition) {
fragment = new Fragment1();
} else {
fragment = new LocationFragment();
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();

LocationFragment 是:

public class LocationFragment extends SupportMapFragment{
private GoogleMap mMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = inflater.inflate(R.layout.map_fragment, container, false);
setUpMapIfNeeded();
return view;
}

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

private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}

private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(60.02532, 30.370552)).title(getString(R.string.map_current_location)));
}
}

xml布局是:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>

问题是,当我们第二次用 LocationFragment 替换 fragment 时,android 无法从 xml 中膨胀。它在线崩溃:

view = inflater.inflate(R.layout.map_fragment, container, false);

有异常(exception):

12-17 01:47:31.209: ERROR/AndroidRuntime(4679): FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #4: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at my.package.LocationFragment.onCreateView(LocationFragment.java:29)
...

而且我不知道如何解决它。我认为这可能与一个旧问题有关,即您不能使用多个 map View (在 android map v1 上发现问题)。但是在 v2 和 fragment 支持的情况下,它必须是用映射 fragment 替换 fragment 容器的方法,对吗?

最佳答案

将您的 mapview fragment 移除到 onDestroyView 方法中。

Fragment fragment = (getFragmentManager().findFragmentById(R.id.mapview));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();

关于第二次充气时出现 Android map v2 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13905977/

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