2014-07-16 19 views
0

我看着這樣的回答:凡當空指針異常名字我的片段:名== NULL

NullPointerException: name == null

,儘量把片段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.OscarJunker.resturantlocaterexample.pcbuilderhandbook.pcbuilderhandbook.FrontFragment"> 

<!-- TODO: Update blank fragment layout --> 
<fragment 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/front_fragment" 
    android:name="com.OscarJunker.resturantlocaterexample.pcbuilderhandbook.pcbuilderhandbook.FrontFragment" 
    /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="@string/hello_blank_fragment" 
    android:layout_gravity="center_horizontal|top" 
    /> 

但我得到這個錯誤:java.lang.IllegalArgumentException:二進制XML文件行#8

我試圖把我的片段與交易應用在Java代碼中

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity"> 

<!-- As the main content view, the view below consumes the entire 
    space available using match_parent in both dimensions. --> 
<FrameLayout 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 

android:name="com.OscarJunker.resturantlocaterexample.pcbuilderhandbook.pcbuilderhandbook.FrontFragment" 

<!-- android:layout_gravity="start" tells DrawerLayout to treat 
    this as a sliding drawer on the left side for left-to-right 
    languages and on the right side for right-to-left languages. 
    If you're not building against API 17 or higher, use 
    android:layout_gravity="left" instead. --> 
<!-- The drawer is given a fixed width in dp and extends the full height of 
    the container. --> 
<fragment android:id="@+id/navigation_drawer" 
    android:layout_width="@dimen/navigation_drawer_width" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:name="com.OscarJunker.resturantlocaterexample.pcbuilderhandbook.pcbuilderhandbook.NavigationDrawerFragment" 
    tools:layout="@layout/fragment_navigation_drawer" /> 

回答

0

你給這裏的佈局中有一個靜態的片段。您不能用交易替換它。相反,您需要在您的XML中添加FrameLayout

+0

對不起,我很困惑,XML代碼是我想要放在我的主要活動中的片段。這是什麼意思,它已經有了一個framlayout? – Chimpanse

+0

@OscarJunker您的活動需要一個佈局,而您的片段需要另一個佈局。您的活動應該有一個FrameLayout,它是要動態加載的片段的佔位符。您應該**不**在另一個片段的佈局中有''標籤。 –

+0

我已經有一個在我的主要actvity,看起來像這樣:我試圖把它在那裏,但得到了錯誤:.IllegalArgumentException:二進制XML文件行#8:重複的ID。它可能是我的導航抽屜我不知道 – Chimpanse