2015-02-09 155 views
1

我想简单的添加到导航抽屉菜单。添加按钮,导航抽屉片段

这是main_activity.xml

<fragment android:id="@+id/navigation_drawer" 
     android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:name="eu.foulidis.giannis.agiospaisios.NavigationDrawerFragment" 
     tools:layout="@layout/fragment_navigation_drawer" /> 

片段UI片段定义:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 


    <Button 
     android:layout_width="wrap_content" 
     android:id="@+id/btnLogin" 
     android:text="@string/com_facebook_picker_done_button_text" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" /> 

    <ListView 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" android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" android:dividerHeight="0dp" 
     android:background="#ff6e1f18" tools:context=".NavigationDrawerFragment" 
     android:layout_above="@id/btnLogin" 
     android:layout_alignParentTop="true" 
     android:id="@+id/myListView" 
     /> 
</RelativeLayout> 


@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     mDrawerListView = (ListView) inflater.inflate(
       R.layout.fragment_navigation_drawer, container, false).findViewById(R.id.myListView); 

没有它的工作原理的按钮,但是当我加入我的异常按钮:

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

任何想法可能是什么问题?

+0

'(ListView控件)inflater.inflate( R.layout.fragment_navigation_drawer,容器,FALSE);'你肯定:'fragment_navigation_drawer'可以转换为'listView'?我认为这里有些问题!添加一个按钮后,它是一个'relativelayout' – 2015-02-10 01:08:33

+0

是的,我也试过'findViewById',同样的错误。 – giannisf 2015-02-10 08:43:49

+0

当然,'findViewById'不能帮忙。我的意思是你可能有铸造问题。这应该很容易解决。 – 2015-02-10 21:52:53

回答

0

难道是因为你在XML文件中两次宣布了Android命名空间(XMLNS)?

我从ListView中删除命名空间,并将它们放在父RelativeLayout中,它适用于我。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 


    <Button 
     android:layout_width="wrap_content" 
     android:id="@+id/btnLogin" 
     android:text="@string/com_facebook_picker_done_button_text" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" /> 

    <ListView android:layout_width="match_parent" 
     android:layout_height="match_parent" android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" android:dividerHeight="0dp" 
     android:background="#ff6e1f18" tools:context=".NavigationDrawerFragment" 
     android:layout_above="@id/btnLogin" 
     android:layout_alignParentTop="true" 
     android:id="@+id/myListView" 
    /> 

</RelativeLayout>