0

Can anyone please help me out with this error. I want list view in Navigation drawer but it throws an error.

Error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
                                                                                         at     com.example.android.logincoupon.DummyActivity.onCreate(DummyActivity.java:29)
                                                                                         at android.app.Activity.performCreate(Activity.java:6251)
                                                                                         at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
                                                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:148) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5461) 
                                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

I am placing my code here.

This is main activity

DummyActivity.java

package com.example.android.logincoupon;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import com.example.android.logincoupon.adapter.NavlistAdapter;
import com.example.android.logincoupon.utils.NavList;

import java.util.ArrayList;

public class DummyActivity extends AppCompatActivity {
DrawerLayout drawerLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dummy);
    drawerLayout=(DrawerLayout) findViewById(R.id.drwar);

    final ArrayList<NavList> navListsitem = new ArrayList<NavList>();
    navListsitem.add(new NavList(R.drawable.ic_manicon, "Person"));
    navListsitem.add(new NavList(R.drawable.ic_toys_black_24dp, "Toys"));
    navListsitem.add(new NavList(R.drawable.ic_call_black_24dp, "Contacts"));
    navListsitem.add(new NavList(R.drawable.ic_transfer_within_a_station_black_24dp,"Transfer"));
    NavlistAdapter adapter = new NavlistAdapter(this, R.layout.navlist_item, navListsitem);
    ListView listview = (ListView) findViewById(R.id.list_nav_item);
    listview.setAdapter(adapter);
}

}

activity_dummy.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout android:id="@+id/drwar"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">


<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        android:gravity="center_vertical"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        />
</LinearLayout>


<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_navigationdrawer"
    />


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

nav_header_navigationdrawer.xml //for navigation drawer item

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"

    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        app:srcCompat="@android:drawable/sym_def_app_icon" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Android Studio"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"         />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android.studio@android.com" />


</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="vertical">

    <ListView
        android:id="@+id/list_nav_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>


</LinearLayout>
</LinearLayout>

navlist_item.xml //for holding the navigation drawer list view

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    android:id="@+id/navlist">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/icon_img"

        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txt_list"
        android:layout_toRightOf="@+id/icon_img"
        android:layout_margin="4dp"/>

</RelativeLayout>

Adapter class NavlistAdapter.java

package com.example.android.logincoupon.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.android.logincoupon.R;
import com.example.android.logincoupon.utils.NavList;

import java.util.List;

public class NavlistAdapter extends ArrayAdapter<NavList>  {
    List<NavList> navLists;
    Context context;
    int layoutResID;


    public NavlistAdapter(@NonNull Context context, @LayoutRes int layoutResID, @NonNull List<NavList> objects) {
        super(context, layoutResID, objects);
        this.context=context;
        this.layoutResID=layoutResID;
        this.navLists=objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View listViewView;
        listViewView=convertView;
        if(listViewView==null) {
            listViewView = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.navlist_item, parent, false);
        }
            NavList navlist=getItem(position);
            TextView listTv=(TextView) listViewView.findViewById(R.id.txt_list);
            listTv.setText(navlist.getWord());
            ImageView listImg=(ImageView) listViewView.findViewById(R.id.icon_img);
            listImg.setImageResource(navlist.getId());

        return listViewView;
    }
}

NavList.java

package com.example.android.logincoupon.utils;

public class NavList {
String word;
int id;

public NavList(int mId, String mWord) {
    id = mId;
    word = mWord;

}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getWord() {
    return word;
}

public void setWord(String word) {
    this.word = word;
}
}
  • Since you have it in the `NavigationView`'s header, you have to find it a little differently. The post linked at the top of your question shows how. – Mike M. May 28 '17 at 10:30
  • Why aren't you using default android navigation activity? it pre-build everything plus it's managed code so you don't need to worry about setting/getting adapters and stuff. only if it fits your need then it's alright. but if you require simple drawer navigation you should go for navigation drawer activity template from create new activity window – Usman Ghauri May 28 '17 at 11:23
  • @MikeM. I am not able to set adapter for Listview. In navigation drawer I am not able to display the list view the app crashes. – Simran jain May 28 '17 at 11:43
  • Yeah, `listview` is null. As mentioned, since it's in the `NavigationView`'s header, that `findViewById()` call won't find it in `onCreate()`. You have to get it directly from the header: `NavigationView nv = (NavigationView) findViewById(R.id.nav_view); View header = nv.getHeaderView(0); ListView listview = (ListView) header.findViewById(R.id.list_nav_item);`. – Mike M. May 28 '17 at 11:47
  • "Not working" doesn't tell us much. If it's the same Exception on the same thing, then I would have to think that what you've posted isn't exactly what you're running. – Mike M. May 28 '17 at 12:29
  • @MikeM. How do I get all the items to be displayed? – Simran jain May 28 '17 at 12:31
  • @MikeM. Only one item is displayed that is the first item, and the rest 3 items? – Simran jain May 28 '17 at 12:38
  • I'd imagine the header and `ListView` aren't tall enough to display all of the items at once. You've got several things in that header. Can you scroll the `ListView`? – Mike M. May 28 '17 at 12:43
  • @MikeM. No I cannot scroll – Simran jain May 28 '17 at 12:51
  • Yeah, that's not surprising, 'cause it's ultimately inside a `RecyclerView`. I'm not sure why you're trying to put a `ListView` in a `NavigationView` header, but if you only have those four items, you'd be better off using a vertical `LinearLayout` instead. Are you sure you're doing what you think you are? A header is the top portion of the `NavigationView`, not the main menu. – Mike M. May 28 '17 at 13:03
  • @MikeM. I want to add list view as menu and not in the nav header. – Simran jain May 28 '17 at 13:19
  • `NavigationView` is designed with a scrolling menu built in. To use that, you need to provide it with a `menu` resource, not try to add a `ListView` to it. There are examples in [this post](https://stackoverflow.com/q/30586725). – Mike M. May 28 '17 at 13:31
  • @MikeM. I have increased the height of the list view. Thank You for the help. – Simran jain May 28 '17 at 13:43

0 Answers0