使用 Google 地图时,应用程序因 FragmentManager Replace 而崩溃

2023-12-11

我使用以下代码来加载片段,具体取决于在 NavigationDrawer 上选择的项目,但是 MapFragment 内部包含 Google 地图片段,并且在第二次尝试打开它后崩溃。

这是我正在使用的代码:

@Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        if (position == 0) {
            fragmentManager.beginTransaction()
                    .replace(R.id.container, MapFragment.newInstance("a", "b"))
                    .commit();

        }
        else {
            fragmentManager.beginTransaction()
                    .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                    .commit();
        }
    }

这是日志:

03-01 14:18:25.870  27175-27175/com.br.appname E/﹕ Device driver API match
    Device driver API version: 23
    User space API version: 23
03-01 14:18:25.870  27175-27175/com.br.appname E/﹕ mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct 30 09:36:10 KST 2013
03-01 14:19:12.655  27175-27175/com.br.appname E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.view.InflateException: Binary XML file line #13: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at com.br.appname.fragments.MapFragment.onCreateView(MapFragment.java:74)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalArgumentException: Binary XML file line #13: Duplicate id 0x7f070055, tag null, or parent id 0x7f070054 with another fragment for com.google.android.gms.maps.MapFragment
            at android.app.Activity.onCreateView(Activity.java:4971)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at com.br.appname.fragments.MapFragment.onCreateView(MapFragment.java:74)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
            at dalvik.system.NativeStart.main(Native Method)

这种情况仅发生在 MapFragment 中。 PlaceholderFragment 工作得很好。

UPDATE

片段映射.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.br.remotepark.fragments.MapFragment">

    <com.sothree.slidinguppanel.SlidingUpPanelLayout
        android:id="@+id/sliding_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#eee"
            android:orientation="vertical"
            android:clickable="true"
            android:focusable="false">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="68dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/name"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="14sp"
                    android:gravity="center_vertical"
                    android:paddingLeft="10dp" />

                <Button
                    android:id="@+id/follow"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:textSize="14sp"
                    android:gravity="center_vertical|right"
                    android:paddingRight="10dp"
                    android:paddingLeft="10dp" />

            </LinearLayout>
        </LinearLayout>

    </com.sothree.slidinguppanel.SlidingUpPanelLayout>

</RelativeLayout>

重复 ID 问题与使用嵌套片段的不正确方式有关。

From: http://developer.android.com/about/versions/android-4.2.html#NestedFragments

Note:当布局包含<fragment>。仅当动态添加到片段时才支持嵌套片段。

So your fragment_map.xml不能直接包含com.google.android.gms.maps.MapFragment。将其替换为FrameLayout带身份证map_container并尝试这样的事情:

public class MyFragmentWithMap extends Fragment {

    private MapFragment fragment;
    private GoogleMap map;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_map, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FragmentManager fm = getChildFragmentManager();
        fragment = (MapFragment) fm.findFragmentById(R.id.map_container);
        if (fragment == null) {
            fragment = MapFragment.newInstance();
            fm.beginTransaction().replace(R.id.map_container, fragment).commit();
        }
    }

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

    // ...
}

注意使用getChildFragmentManager.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Google 地图时,应用程序因 FragmentManager Replace 而崩溃 的相关文章

  • Android:在对话框中使用日期选择器和时间选择器

    我面临一个我不知道如何解决的问题 我有一个活动 当我单击链接到该活动的菜单的特定项目时 会显示一个对话框 用于添加项目 该项目有日期和时间 但我无法获得DatePicker and TimePicker在此对话框中 我还尝试将活动传递给对话
  • 为什么我不应该在 android 中使用 System.out.println()

    In the Android开源项目的代码风格 http source android com source code style html 它指出我们不应该使用System out println 但我不明白为什么 谁能解释一下吗 我应该
  • ListView 滚动时隐藏和显示浮动按钮

    我正在我的应用程序中创建一个 listView 和一个浮动按钮 我希望根据滚动状态实现隐藏和返回的效果 当 的时候ListView滚动时按钮隐藏得很好 但是当滚动停止时按钮不会返回到其初始位置 有任何想法吗 My Code public c
  • Xamarin:找不到“libmonodroid.so”模拟器错误

    我已经遇到这个问题几天了 但还没有找到解决方案 我一直在使用Android 7 0 ARM armeabi v7a 我的模拟器的图像 因为它太慢了 我一直在尝试使用Android 7 0 Intel Atom x86 64 with HAX
  • Android模拟器收不到推送通知

    我在谷歌云上使用推送通知 但是由于某种原因我无法在模拟器上接收推送通知 然而 当我在真实设备上测试时 同一个应用程序确实会收到通知 有其他人遇到过这个问题或找到解决方案吗 模拟设备具有网络访问权限 并且与真实设备具有相同的 Android
  • 如何在Android中设置对话框的图标

    我想在Android中自定义一个对话框 我知道如何设置对话框的标题 dialog setTitle O message 现在我想在标题前面设置图标 我怎样才能实现这个目标 Dialog dialog dialog new Dialog th
  • 如何减少 Android 布局中文本之间的行距?

    我想减少所附布局中文本之间的行距 User12 5 部电影 改进 2 5 下面是 xml 我尝试删除 singleLine true 语句并尝试将其设置为 false
  • 自定义溢出图标太大(使用 android.support.v7 和 AppCompat)

    我的目标是让操作栏为红色 但由于默认 Android 库中我的唯一选项是白色和黑色 因此我尝试对其进行自定义 我通过在 gimp 上使用油漆桶来填充白色图标并以不同的名称重新保存它 从而制作了一些自定义红色图标 我对溢出图标做了同样的事情
  • 为什么签名的 Android apk 无法在模拟器上运行

    我已经制作了一个android项目的签名apk 每当我的客户尝试在模拟器上运行它时 他都会遇到以下错误消息 D Android android sdk windows tools gt adb install r abc apk 500 K
  • 3D 旋转 - 透视

    public class MainActivity extends Activity LinearLayout rotator protected void onCreate Bundle savedInstanceState super
  • 从滚动视图返回子视图

    我有一个父母ScrollView带有儿童视图 当用户按下后退按钮时 我希望子视图处理该事件 我尝试了一些方法 但似乎都不起作用 按后退按钮会终止活动 public class GameScrollView extends ScrollVie
  • 如何在首次运行时填充大型 SQLite 数据库

    我正在开发一个基于 SQLite 数据库的字典应用程序 该数据库包含超过 300 000 行 问题在于 最终形式的数据库文件由全文索引表组成 并且重量远远超过150Mb 我通过创建无内容的 fts4 表设法将 db 文件大小降至最低 数据库
  • 直接在布局 xml 中填充微调器

    是否可以在布局 xml 中直接填充 Spinner 的选项 这一页 http developer android com guide tutorials views hello spinner html建议我应该使用 ArrayAdapte
  • 如何从代码中检测 Android 设备上的双核 cpu?

    我遇到了一个似乎仅影响双核的问题Android设备运行Android 2 3 姜饼 或更大 我想就这个问题进行一次对话 但仅限于符合该标准的用户 我知道如何检查OS级别 但还没有找到任何可以明确告诉我该设备正在使用多核的信息 有任何想法吗
  • 如何获取 Android 版 Chromium 的源代码

    我正在尝试为 Android 构建 Chromium 开源浏览器 我已经提到过 Visit http code google com p chromium wiki AndroidBuildInstructions http code go
  • 多少次函数调用会导致堆栈溢出

    你好 Android Java 开发者 当一个函数调用一个函数并且该函数调用另一个函数等等时 有多少次调用 堆栈长度 会让我陷入堆栈溢出 有一般经验法则吗 我问的原因是因为我现在对于我的 5 人纸牌游戏来说哪个更有效 设计明智 解决方案一
  • Android 基础知识:在 UI 线程中运行代码

    从在 UI 线程中运行代码的角度来看 以下两者之间有什么区别 MainActivity this runOnUiThread new Runnable public void run Log d UI thread I am the UI
  • 注意通知持续时间

    是否可以将抬头通知的持续时间设置为无限 现在它只显示 5 秒 已经尝试过不同的事情 例如更改类别 但持续时间始终为 5 秒 这是我的代码 Notification notification notificationBuilder setCa
  • 错误:找不到 com.google.firebase.storage.UploadTask.TaskSnapshot 类型的符号方法 getDownloadUrl()

    在 gradle update 之前一切正常 但后来弹出了这个错误 我参考了官方文档 它提供了相同的代码 不接受 getDownloadUrl 方法 https i stack imgur com YLC2x png 我已经添加了最新的正确
  • 如何处理 Android M 中广播接收器权限的删除?

    我有一些遗留代码 我正在为棉花糖提供安全许可 有一个使用PHONE STATE权限的广播如下

随机推荐