Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 活动顶部未显示片段_Android_Android Fragments - Fatal编程技术网

Android 活动顶部未显示片段

Android 活动顶部未显示片段,android,android-fragments,Android,Android Fragments,我想在将项目单击到操作栏时显示一个片段,在代码中我这样做: @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.create_boundary: Fragment boundary = new BoundaryFragment();

我想在将项目单击到操作栏时显示一个片段,在代码中我这样做:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.create_boundary:
                Fragment boundary = new BoundaryFragment();
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.map_activity, boundary);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.addToBackStack(null);
                ft.commit();
             break;
        }
    }
片段:

public class BoundaryFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // create the view of the fragment associate with the maps activity
        View view = inflater.inflate(R.layout.boundary_maps, container, false);
        return view;
    }
片段的布局:

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

</GridLayout>
我不明白为什么没有显示。有人能告诉我出了什么问题吗


提前感谢。

如官方文件所述:

参数
-
containerWebID
所属容器的标识符 碎片将被替换。
-
片段
要放置的新片段 容器。
-
标记
片段的可选标记名,请稍后参阅 使用
FragmentManager.findFragmentByTag(字符串)
检索片段

这意味着您在交易中使用的
id
不正确。另外,您可能希望在XML布局中定义的片段之上添加
BoundaryFragment
,因此您应该使用

要解决您的问题,您应该将
id
移动到
相对位置,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map_activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <fragment android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>

首先,您使用的是片段,因为它将是一个视图组。执行下一条语句时,您正试图将一个片段放入另一个片段中:

ft.replace(R.id.map_activity, boundary);
其次,不能替换布局文件中定义的片段。只能替换使用FragmentTransaction动态添加的片段

要实现您想要的功能,请参阅部分下的示例代码:
“或者,以编程方式将片段添加到现有的视图组。”

您可以发布相对于R.id.map\u活动id的xml代码吗?@valbertos刚刚完成。@valbertos您认为有什么问题吗?您明确地将片段绑定到
中的类。尝试用
@AlexanderZhak替换
,谢谢你的评论,你能解释一下我为什么要做这个更改吗?你可能想把BoundaryFragment放在另一个上面。因此,你应该使用
FragmentTransaction.add()
而不是
.replace()
我想这样做。我做了很多测试,但都失败了。因为如果我把
android:id=“@+id/map\u activity
在relativeLayout内部,activity的
onCreate
方法崩溃,因为它找不到id.acitivity\u map会导致
nullPointExecption
,因此我必须将id插入
片段标记
,这是加载
activity\u map
布局的唯一方法,但是用这种方式我不能添加或替换新的片段。NPE是一种症状,表明您正在活动中的某个地方使用id来获取映射片段。只需在RelativeLayout上定义另一个id,并在事务中使用它。使用两个id,无论如何都不起作用。请参阅问题更新以查看新的xml。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map_activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <fragment android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>
ft.replace(R.id.map_activity, boundary);