错误从xml中膨胀类别片段

问题描述:

我在这里发现了几个类似的问题,但提出的答案不适合我。错误从xml中膨胀类别片段

所以,错误的是:

08-13 14:40:10.723: E/AndroidRuntime(630): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragments/com.example.fragments.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment 

我的MainActivity是:

package com.example.fragments; 

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 

public class MainActivity extends FragmentActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

} 

我的XML是:

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

    <fragment 
     android:id="@+id/listFragment" 
     android:layout_width="150dip" 
     android:layout_height="match_parent" 
     android:layout_marginTop="?android:attr/actionBarSize" 
     class="com.example.fragments.ListFragment" > 
    </fragment> 

    <fragment 
     android:id="@+id/detailFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.example.fragments.DetailFragment" > 
    </fragment> 

</LinearLayout> 

会很感激的任何帮助关于这问题

xx

+0

是类com.example.fragments.ListFragment ,com.example.fragments.DetailFragment存在于你的应用程序中? – nandeesh 2012-08-13 15:14:05

+0

亲爱的nandeesh,是的,他们目前在项目文件夹中,并在包com.example.fragments – Dennis 2012-08-13 16:04:08

所以基本上这个问题是通过实现所有片段解决 - 在项目中有关的进口如下:

import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.ListFragment 

不是:

import android.app.Fragment 
import android.app.FragmentActivity 
import android.app.ListFragment 
+0

我是新来的Android,但不是在类android.support.v4.app只包装更新的Android功能的backports,所以你可以使用旧版Android的新功能?如果更改导入工程,它的工作原理。但我仍然很想知道为什么android.app实现 – spaaarky21 2012-12-14 21:46:01

+0

你的问题是100%公平,但诚实 - 我不知道:)一种方法为我工作,其他没有。那么简单:)那时我没有足够的时间去探索细节。对不起xx – Dennis 2013-01-02 10:14:47

+0

原因是您的活动扩展了android.support.v4.app.FragmentActivity。 android.support.v4.app和android.app是单独的类树,所以Fragment在另一个中可能看起来像Fragment,但它们是单独的对象。如果你检查它们的继承层次结构,它们会在Object之后发生分离。只有在需要时才使用支持库,并避免此问题。 – majinnaibu 2013-07-04 00:08:49

我认为可能会导致

android:layout_marginTop="?android:attr/actionBarSize" 

actionBarSize只引入API 11的错误,当你正在使用的支持库我假设你预先的Android 3.0设备上的发展。尝试删除该属性,看看它是否是问题。

+0

刚刚尝试删除该属性 - 没有帮助:( – Dennis 2012-08-13 16:02:26