Java 使用片段时发生异常
我试图在活动中创建一个片段,但出现以下异常: java.lang.IllegalArgumentException:找不到id的视图 这是activity_main.xmlJava 使用片段时发生异常,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
<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>