当NullPointerException:name == null时,在哪里命名我的片段

时间:2014-07-16 00:22:22

标签: java android android-fragments

我看了这个答案:

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" />

1 个答案:

答案 0 :(得分:0)

你在这里给出的布局中有一个静态片段。您无法将其替换为交易。相反,您需要在XML中添加FrameLayout