从活动中膨胀片段

Nih*_*uri 3 android android-fragments

这是我的活动课。我正在尝试将片段膨胀到此活动。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fg1, new ButtonsFragment());
        ft.commit();
    }
Run Code Online (Sandbox Code Playgroud)

活动_main.xml

<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/a_layout"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_margin="10dp"
        android:id="@+id/fg1"
        />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

ButtonsFragment.java

public class ButtonsFragment extends Fragment {

    Button b1,b2,b3;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
    {
        super.onCreateView(inflater,container,savedInstanceState);

       View v = inflater.inflate(R.layout.fragment_buttons,container,false);

        b2 = (Button)v.findViewById(R.id.b2);
        b1 = (Button)v.findViewById(R.id.b1);
        b3 = (Button)v.findViewById(R.id.b3);
        return v;
    }
}
Run Code Online (Sandbox Code Playgroud)

fragment_buttons.xml

<FrameLayout 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.example.iiitb.patientdoctormanagementsystem.ButtonsFragment">

 <LinearLayout
        android:id="@+id/ll"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:textAllCaps="false"
            android:text="Past Appointments"
            android:id="@+id/b1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />

        <Button
            android:textAllCaps="false"
            android:text="Todays Appointments"
            android:id="@+id/b2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />

        <Button
            android:textAllCaps="false"
            android:text="Upcoming Appointments"
            android:id="@+id/b3"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
    </LinearLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

但我收到此异常:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iiitb.patientdoctormanagementsystem/com.example.iiitb.patientdoctormanagementsystem.MainActivity}: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class fragment 
Run Code Online (Sandbox Code Playgroud)

请帮忙!!

jib*_*him 5

有两种方法可以将片段添加到活动布局:

  • 在活动的布局文件中声明片段
  • 以编程方式将片段添加到 ViewGroup

在您的示例中,您同时遵循了两种方法。您应该选择其中之一。当您在活动布局中添加片段时:

<fragment
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_margin="10dp"
    android:id="@+id/fg1"
    />
Run Code Online (Sandbox Code Playgroud)

你错过了android:name属性。此属性指定要在布局中实例化的 Fragment 类。所以,你应该这样声明:

 <fragment
    android:name="your.package.ButtonsFragment"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_margin="10dp"
    android:id="@+id/fg1"
    />
Run Code Online (Sandbox Code Playgroud)

当系统创建此活动布局时,它会实例化布局中指定的每个片段并为每个片段调用 onCreateView() 方法,以检索每个片段的布局。系统直接插入片段返回的View代替元素。(来自developer.android.com)

通过遵循这种方法,您不需要这些代码行(将其删除):

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fg1, new ButtonsFragment());
ft.commit();
Run Code Online (Sandbox Code Playgroud)

如果您想遵循第二种方法,以编程方式将片段添加到现有的 ViewGroup,您应该按如下方式更改活动布局:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_margin="10dp"
    android:id="@+id/fg1" />
Run Code Online (Sandbox Code Playgroud)

并保留代码的其他部分。有关片段的更多信息,您可以参考 android文档