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;
}
}