2012-08-13 19 views
1

我在這裏發現了幾個類似的問題,但提出的答案不適合我。錯誤從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

回答

4

所以基本上這個問題是通過實現所有片段解決 - 在項目中有關的進口如下:

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

1

我認爲可能會導致

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

actionBarSize只引入API 11的錯誤,當你正在使用的支持庫我假設你預先的Android 3.0設備上的發展。嘗試刪除該屬性,看看它是否是問題。

+0

剛剛嘗試刪除該屬性 - 沒有幫助:( – Dennis 2012-08-13 16:02:26