【问题标题】:Transaction from mapfragments to other fragments从mapfragments到其他fragment的事务
【发布时间】:2013-06-23 16:07:08
【问题描述】:

我在将地图片段更改为其他片段时遇到了问题。 (列表片段或文本) 当我对只有文本的片段使用片段事务时。地图还在,新的片段在地图上。我无法删除地图。

我搜索了它,但没有找到好的方法。 有人可以提供一些想法吗?

这是主要的活动。它扩展了活动 在此输入代码

protected void onCreate(Bundle savedInstanceState) {
      if (android.os.Build.VERSION.SDK_INT > 9) {
          StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
          StrictMode.setThreadPolicy(policy);
        }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
FragmentManager myfm = getFragmentManager();
FragmentTransaction ft = myfm.beginTransaction();
mymap = new StartFragment();
    ft.add(R.id.map, mymap);
    ft.commit();

这是activity_main中的片段;

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

这是 start_fragment 类

public class StartFragment extends Fragment  {
@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle     savedInstanceState) { 
      View view = inflater.inflate(R.layout.map_layout,null);
    return view;
 } 
 }

然后是map_layout

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/map"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 class="com.google.android.gms.maps.MapFragment"/>

谢谢!

【问题讨论】:

  • 为什么要将MapFragment替换为另一个包含MapFragment的片段?

标签: android fragment google-maps-android-api-2 mapfragment


【解决方案1】:

您的代码中有几个问题。

首先,不能替换xml中添加的片段。您只能在代码中添加此片段,然后使用FragmentTransaction.replace 切换到其他片段。

另一件事是您不能在 xml 中的片段内添加片段。阅读此http://developer.android.com/about/versions/android-4.2.html#NestedFragments 了解更多详情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多