2

我想LinearLayout在“onCreateView”方法中将 a 充气到一个片段中。但我总是犯同样的错误:

"android.view.InflateException: Binary XML file line #7: Error inflating class fragment"

android.support.v4.app.FragmentActivity"libs"文件夹中使用。

主要活动:

public class GeofranceDeptActivity extends FragmentActivity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dept);

    }
}

主xml文件dept

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <fragment class="dauran.geofrance.DeptFragment"
            android:id="@+id/depts" android:layout_weight="1"
            android:layout_width="0px"
            android:layout_height="match_parent"> 
            </fragment>

    <FrameLayout android:id="@+id/detailsdept" android:layout_weight="1"
            android:layout_width="0px"
            android:layout_height="match_parent" />

</LinearLayout>

片段的类DeptFragment

public class DeptFragment extends Fragment implements OnTouchListener{

    Context mContext = getActivity();

    boolean mDualPane;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
              return null;
            }

    return (LinearLayout)inflater.inflate(R.layout.dept_fragment, container, false);
        }


    @Override
    public void onActivityCreated(Bundle savedState) {
        ................etc

dept_fragment我要膨胀的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical" >
    <ImageView
        android:id="@+id/targetDeptImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/region_france_grand" />

    <ImageView
        android:id="@+id/cacheDeptImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/region_france_grand" />
    <Spinner
        android:id="@+id/sDepartement"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/targetImage" />

</RelativeLayout>
</LinearLayout>

Logcat: _

06-26 19:02:27.940: E/AndroidRuntime(11993): FATAL EXCEPTION: main
06-26 19:02:27.940: E/AndroidRuntime(11993): java.lang.RuntimeException: Unable to start activity ComponentInfo{dauran.geofrance/dauran.geofrance.GeofranceDeptActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.os.Looper.loop(Looper.java:137)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.ActivityThread.main(ActivityThread.java:4424)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at java.lang.reflect.Method.invokeNative(Native Method)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at java.lang.reflect.Method.invoke(Method.java:511)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at dalvik.system.NativeStart.main(Native Method)
06-26 19:02:27.940: E/AndroidRuntime(11993): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.Activity.setContentView(Activity.java:1835)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at dauran.geofrance.GeofranceDeptActivity.onCreate(GeofranceDeptActivity.java:10)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.Activity.performCreate(Activity.java:4465)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-26 19:02:27.940: E/AndroidRuntime(11993):    ... 11 more
06-26 19:02:27.940: E/AndroidRuntime(11993): Caused by: java.lang.IllegalStateException: Fragment dauran.geofrance.DeptFragment did not create a view.
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:293)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
4

1 回答 1

2

Logcat可能比android.view.InflateException: Binary XML file line #7: Error inflating class fragment说的更多(检查下面是否没有实例化异常,也许你可以添加完整的异常?)。查看您的代码后,我会假设您DeptFragment不在dauran.geofrance包中,并且由于这个 android 无法找到它。

null此外,无需测试container参数:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    return inflater.inflate(R.layout.dept_fragment, container, false);
}

此外,我不认为需要用RelativeLayouta包装:LinearLayoutR.layout.deptfragment

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
    <ImageView
        android:id="@+id/targetDeptImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/region_france_grand" />

    <ImageView
        android:id="@+id/cacheDeptImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/region_france_grand" />
    <Spinner
        android:id="@+id/sDepartement"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/targetImage" />

</RelativeLayout>
于 2012-06-26T16:13:15.777 回答