1

我尝试使用线性布局将 edittext 字段添加到导航抽屉,如以下代码所示。

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

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

<LinearLayout
    android:id="@+id/left_drawer"
    android:layout_width="320dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/EditText01"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:hint="Search" >
    </EditText>

    <ListView
        android:id="@+id/left_drawer_child"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</LinearLayout>

</android.support.v4.widget.DrawerLayout>

但是当我试图填充列表视图时,它给了我一个空指针异常。这是我使用的代码

DrawerLayout mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
LinearLayout mDrawerLinear = (LinearLayout) findViewById(R.id.left_drawer);
ListView mDrawerListChild = (ListView) findViewById(R.id.left_drawer_child);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.drawer_listview_item, drawerListViewItems);
mDrawer.closeDrawer(mDrawerLinear);

错误:

Caused by: java.lang.NullPointerException

我在这里做错了什么?

谢谢!

4

2 回答 2

0

试试这个,

listView.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.drawerItem)));

于 2016-04-01T07:12:50.560 回答
-1

似乎你String[] drawerListViewItemsnull在将它传递给ArrayAdapter.

在您strings.xml的字符串数组中定义应显示的内容

...
<string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
        <item>Jupiter</item>
        <item>Saturn</item>
        <item>Uranus</item>
        <item>Neptune</item>
</string-array> 
...

并在将其传递给之前drawerListViewItems用此内容填充您的 contentArraystrings.xmlArrayAdapter

...
String[] mPlanetTitles = getResources().getStringArray(R.array.planets_array);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.drawer_listview_item, mPlanetTitles );
...

样本取自:http: //developer.android.com/training/implementing-navigation/nav-drawer.html#Init

于 2013-10-29T09:28:23.637 回答