Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有列表视图的导航抽屉。ListView上的空点异常_Android_Xml_Listview_Android Listview_Navigation Drawer - Fatal编程技术网

Android 带有列表视图的导航抽屉。ListView上的空点异常

Android 带有列表视图的导航抽屉。ListView上的空点异常,android,xml,listview,android-listview,navigation-drawer,Android,Xml,Listview,Android Listview,Navigation Drawer,我遵循这一点,为抽屉菜单和选项卡布局。因此,我为菜单创建了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:l

我遵循这一点,为抽屉菜单和选项卡布局。因此,我为菜单创建了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>

这是主要的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;
}
}
public类MainActivity扩展了AppCompatActivity{
阵列适配器;
列表=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView ListView=(ListView)findViewById(R.id.ListView);
adapter=newmyadapter(这个,getModel());
setAdapter(适配器);
}
私有列表getModel(){
添加(新型号(“iPezione”);
退货清单;
}
}

我在ListView上有一个空点验证。所以我知道,因为我有这个问题(我有同样的问题,碎片),但我不能解决它与菜单。我该怎么办?

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

像这样吗?是的,如果您按照该教程中的步骤操作,那么应该可以正常工作。制作导航抽屉有两种方法,第一种是我链接的第一个教程,第二种是我链接的另一个教程。。secondo方法已经完成,并且工作正常。我会测试第一个方法,没有项,但是有一个listVew,但是我不能在ListView中使用引用。。