Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.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 找不到片段id的视图,即使FrameLayout是主布局的子级_Android_Android Fragments - Fatal编程技术网

Android 找不到片段id的视图,即使FrameLayout是主布局的子级

Android 找不到片段id的视图,即使FrameLayout是主布局的子级,android,android-fragments,Android,Android Fragments,我已经创建了代码,以便在点击菜单按钮时动态添加片段。但是我得到了一个错误: 未找到片段XYPlotFragment{a75aa2e#0 id=0x7f0d009a}的id 0x7f0d009a(tech.glasgowneuro.attysecg:id/fragment_container2)的视图 我已经阅读了这里的所有其他帖子,std的答案是框架布局需要是主布局的子级。我想我做得对。仍然找不到id 活动\u plot\u window.xml: <?xml version="1.0"

我已经创建了代码,以便在点击菜单按钮时动态添加片段。但是我得到了一个错误: 未找到片段XYPlotFragment{a75aa2e#0 id=0x7f0d009a}的id 0x7f0d009a(tech.glasgowneuro.attysecg:id/fragment_container2)的视图 我已经阅读了这里的所有其他帖子,std的答案是框架布局需要是主布局的子级。我想我做得对。仍然找不到id

活动\u plot\u window.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">


    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <tech.glasgowneuro.attysecg.InfoView
                android:id="@+id/infoview"
                title="Plot Window"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp" />

            <tech.glasgowneuro.attysecg.RealtimePlotView
                android:id="@+id/realtimeplotview"
                title="Plot Window"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp" />

        </RelativeLayout>

        <FrameLayout
            android:id="@+id/fragment_container2"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</RelativeLayout><!--    </LinearLayout> -->
问题是为什么我会得到这个例外?如果执行findViewById(fragment_container2),则结果为null。 如何获取片段容器2的有效id?
在这一点上,视图肯定是膨胀的,因为我在应用程序启动很久之后触发了此崩溃。

您需要通过FragmentManager找到它:

getSupportFragmentManager().findFragmentById(R.id.fragment_container2);

我已经弄明白了。Android studio创建了两个“activity\u plot\u window.xml”文件。其中一个有后缀_v21,因此旧API和新API有两个版本。这里解释如下:
我已经将Framelayout添加到了_v21文件中,现在它可以工作了。当不展开时(仅为灰色(2)),很容易进行监视。

您的意思是说您的视图已正确膨胀,并在某些操作引发此次崩溃之前显示出来。从您所提到的,这个问题与您试图放入的片段无关。框架布局根本不存在。您能提供更多CASE语句所在代码的快照吗?这是关于向容器中添加一个片段,这是行不通的。这不是为了得到一个指向片段的指针。我想将片段plotFragment添加到容器中。但是片段管理器拒绝这样做。
getSupportFragmentManager().findFragmentById(R.id.fragment_container2);