设置listview适配器时的NullPointerException(DrawerLayout)

时间:2016-05-09 10:47:27

标签: java android listview android-fragments drawerlayout

我有一个片段布局,它是抽屉,看起来像这样:

fragment_overview.xml

<?xml version="1.0" encoding="utf-8"?>
<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">

<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffe887" />

<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="multipleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

该片段的java类:

OverviewFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_overview, container, false);

    mlistOptions = getResources().getStringArray(R.array.list_options);
    mDrawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) getActivity().findViewById(R.id.left_drawer);

    // Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<String>(getActivity(),
            R.layout.simple_list_item_1, mlistOptions));

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    return root;
}

在资源文件夹中使用此代码:

<string-array name="list_options">
    <item>Ship</item>
    <item>Map</item>
    <item>Mail</item>
    <item>Settings</item>
    <item>Logout</item>
</string-array>

这是一个列表项:

simple_list_item_1.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
android:textColor="@android:color/black"
android:textStyle="italic">

但是当setAdapter方法在java类中运行时,它会生成此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

该方法调用实际上可能出现什么问题?它是一个片段,所以我们需要getActivity作为第一个参数,布局文件只是对一个简单文件的引用,而optionslist只是一个来自资源文件夹的arraylist ..什么可以为null?

提前致谢!

3 个答案:

答案 0 :(得分:0)

试试这个,而不是使用getActivity使用root,因为你的组件现在处于root视图中,所以你要扩充你的布局。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_overview, null);

    mlistOptions = getResources().getStringArray(R.array.list_options);
    mDrawerLayout = (DrawerLayout) root.findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) root.findViewById(R.id.left_drawer);

    // Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<String>(getActivity(),
            R.layout.simple_list_item_1, mlistOptions));

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    return root;
}

答案 1 :(得分:0)

您应该在主要活动中填充抽屉列表。将此代码放在您的活动的onCreate方法中。

然后在您的片段onCreateView中,您需要对另一个将放入(主要活动的)FrameLayout的布局进行充气。

答案 2 :(得分:0)

尝试更换

<a href="#browsealoud" onclick="browsealoud.toggleBar(); return false;" class="dark-arrow" data-bapdf="80"><h6>Screen reader</h6></a>

mDrawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
mDrawerList = (ListView) getActivity().findViewById(R.id.left_drawer);