【问题标题】:Error on inflating a fragment膨胀片段时出错
【发布时间】:2025-11-25 02:00:01
【问题描述】:

我有一个片段,我正在像下面这样膨胀它,但它给出了运行时错误:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View jobsView = inflater.inflate(R.layout.fragment_job_information, container, false);
        textView = (TextView) jobsView.findViewById(R.id.textView);


        if (jobInfo == null) {
            EmployerHistory jobsQuery=new EmployerHistory();
            jobInfo = jobsQuery.getJobInfo(employerName, position);
        }

        textView.setText(jobInfo.get("jobName").toString());

        return jobsView;
    }

其对应的布局是:

<fragment 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" android:id="@+id/jobInfoFragment">
    <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"
        tools:context="com.example.ankit.job_depot.employer.view.JobInformation">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView2"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>
</fragment>

实际上,我已经在带有 listview 的片段上,当用户单击 listview 的元素时,该片段需要被新片段更改,在我的新片段中,我有: l

istViewJobHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                JobInformation newFragment =  new JobInformation();
                android.support.v4.app.FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.listViewJobHistory, newFragment);
                transaction.addToBackStack(null);
                transaction.commit();
            }

        });

我收到以下异常:

07-28 18:49:39.681 14474-14474/com.example.ankit.job_depot E/AndroidRuntime: FATAL EXCEPTION: main 进程:com.example.ankit.job_depot,PID:14474 android.view.InflateException: Binary XML file line #1: Error inflating class fragment 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:482) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:414) 在 com.example.ankit.job_depot.employer.view.JobInformation.onCreateView(JobInformation.java:45) 在 android.support.v4.app.Fragment.performCreateView(Fragment.java:1789) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)

【问题讨论】:

  • 您的 Activity 是否扩展了 FragmentActivity ?
  • 它正在扩展Fragment
  • Fragment 不在Activity 类型层次结构中。

标签: java android listview android-fragments


【解决方案1】:

我认为你的主要活动应该扩展 FragmentActivity

import android.support.v4.app.FragmentActivity;

【讨论】:

  • 是否必须使用FragmentActivity,我不能切换片段的内容,即我希望在选择列表项时更新片段内容。
  • 不,您不必更改片段类,您将拥有一个需要扩展 FragmentActivity 而不是 Activity 的 Activity 类,无需更改片段代码。