android - 从 Activity 中膨胀 fragment

标签 android android-fragments

这是我的 Activity 课。我正在尝试为该 Activity 增加 fragment 。

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();
    }

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

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;
    }
}

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>

但是我遇到了这个异常:

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 

请帮忙!!

最佳答案

有两种方法可以将 fragment 添加到 Activity 布局:

  • 在 Activity 的布局文件中声明 fragment
  • 以编程方式将 fragment 添加到 ViewGroup

在您的示例中,您同时遵循了两个方法。你应该选择其中之一。当您在 Activity 布局中添加 fragment 时:

<fragment
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_margin="10dp"
    android:id="@+id/fg1"
    />

您错过了 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"
    />

When the system creates this activity layout, it instantiates each fragment specified in the layout and calls the onCreateView() method for each one, to retrieve each fragment's layout. The system inserts the View returned by the fragment directly in place of the element. (from developer.android.com)

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

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fg1, new ButtonsFragment());
ft.commit();

如果您想采用第二种方法,以编程方式将 fragment 添加到现有的 ViewGroup,您应该按如下方式更改 Activity 布局:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_margin="10dp"
    android:id="@+id/fg1" />

并保持代码的其他部分不变。有关 fragment 的更多信息,您可以引用 android documentation

关于android - 从 Activity 中膨胀 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47338159/

相关文章:

android - 适用于 ios 和 android 的 Cocos2d-x 和 CocosBuilder 多分辨率

android - 在框架布局中访问动态添加的谷歌地图 fragment

java - 带有适配器 ""的 ViewPager 需要 AsyncTask 中的 View id

android - Wifi socket 与 android 手机通信

android - 戴尔 Streak 7 Eclipse/adb

android - Flutter - 可以在 Flutter 应用程序中导入 fragment 吗?

android - 如何将值从 Fragment 传递到 Activity

android - 这是对 Android Fragments 和 NavigationDrawer 的正确使用吗?

java - 为什么 BindingAdapter 必须是静态方法?

android - 如何按百分比显示进度条状态