Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.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
Java 使用片段时发生异常_Java_Android_Fragment - Fatal编程技术网

Java 使用片段时发生异常

Java 使用片段时发生异常,java,android,fragment,Java,Android,Fragment,我试图在活动中创建一个片段,但出现以下异常: java.lang.IllegalArgumentException:找不到id的视图 这是activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pa

我试图在活动中创建一个片段,但出现以下异常:

java.lang.IllegalArgumentException:找不到id的视图

这是activity_main.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">

    <EditText
        android:id="@+id/editText_str"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText_str"
        android:ems="10" />

    <Button
        android:id="@+id/btn_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText_num"
        android:text="Update" />

</RelativeLayout>
它是MyFragment.java

package com.example.example24;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        String str = getArguments().getString("str");
        int num = getArguments().getInt("num");
        return inflater.inflate(R.layout.content_layout, container, false);
    }
}

您正在使用布局
R.layout.activity\u main
,然后尝试将片段设置到不属于布局的
R.id.Fragment\u容器中。

您需要xml中的视图组,以便通过编程将片段添加到视图组中

使用
add()
代替
replace()

如果要替换添加到容器中的现有片段,请使用
replace()

所以换成

   transaction.add(R.id.fragment_container, newFragment);
传递给
add()
的第一个参数是
ViewGroup
,其中应该放置
片段,由资源ID指定,第二个参数是要添加的片段

    <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">

    <EditText
        android:id="@+id/editText_str"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText_str"
        android:ems="10" />

    <Button
        android:id="@+id/btn_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText_num"
        android:text="Update" />
    <LinearLayout
            android:id="@+id/fragment_container"
            android:layout_below="@+id/btn_update"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
    </LinearLayout>


</RelativeLayout>

请发布您的日志。
   transaction.add(R.id.fragment_container, newFragment);
    <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">

    <EditText
        android:id="@+id/editText_str"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText_str"
        android:ems="10" />

    <Button
        android:id="@+id/btn_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText_num"
        android:text="Update" />
    <LinearLayout
            android:id="@+id/fragment_container"
            android:layout_below="@+id/btn_update"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
    </LinearLayout>


</RelativeLayout>