【问题标题】:Navigation Drawer whith ListView. Null Point Exception on ListView带有 ListView 的导航抽屉。 ListView 上的空指针异常
【发布时间】:2016-03-09 15:01:13
【问题描述】:

我正在关注这个tutorial,以获得抽屉菜单和选项卡布局。所以我为菜单创建了 xml 文件,但我不会使用项目,而是使用 ListView。这是菜单的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/listView"
   android:layout_gravity="center" />
</menu>

这里是活动的xml代码:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/orange"
    android:id="@+id/toolbar"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:title="Drawer With Swipe Tabs" />



<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/drawerLayout"
    >

    <FrameLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/containerView">

    </FrameLayout>



    <android.support.design.widget.NavigationView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:id="@+id/shitstuff"
        app:itemTextColor="@color/black"
        app:menu="@menu/drawermenu"
        android:layout_marginTop="-24dp"
        />

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

这是 MainActivity java 代码:

public class MainActivity extends AppCompatActivity {
ArrayAdapter<Model> adapter;
List<Model> list = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView listView = (ListView)findViewById(R.id.listView);
    adapter = new MyAdapter(this, getModel());
    listView.setAdapter(adapter);

}

private List getModel() {
    list.add(new Model("ISPEZIONE"));
    return list;
}
}

我在 ListView 上有空点异常。所以我知道是因为有问题(我对片段也有同样的问题),但我不能用菜单解决它。我该怎么办?

【问题讨论】:

    标签: android xml listview android-listview navigation-drawer


    【解决方案1】:

    您的活动视图层次结构中没有 ListView,因此 findViewById 返回 null(这会导致您的崩溃)。如果您想使用 ListView 而不是 NavigationView,只需将 NavigationView 元素替换为包含 ListView 的片段/FrameLayout。

    【讨论】:

    • 是的,如果您按照该教程中的步骤操作应该可以正常工作
    • 制作导航抽屉有两种方法,第一种方法是我链接的第一个教程,第二种方法是链接的另一个教程。第二种方法已经完成,它工作正常。我会测试第一种方法,没有项目,但有一个 listVew,但我不能在 ListView 上做一个参考..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    相关资源
    最近更新 更多