Nabia Saroosh Nabia Saroosh - 1 month ago 15
Android Question

NullPointerException: android.widget.ListView.setAdapter(android.widget.ListAdapter)'

I am trying to show data in

drawerLayout
by using
ListView
and
customAdapter
class but I am facing an error and I am confused to find the solution, why is it showing me
NullPointerException
?

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nabia.myapplication/com.example.nabia.myapplication.Activities.FindNearbyMosque}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
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.nabia.myapplication.Activities.FindNearbyMosque.onCreate(FindNearbyMosque.java:94)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
at android.app.ActivityThread.access$900(ActivityThread.java:177) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5942) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372)


My
ListView
code is given below.

public class FindNearbyMosque extends AppCompatActivity {
private List<ItemSlideMenu> itemSlideMenus;
private SlidingMenuAdapter slidingMenuAdapter;
private ListView listviewsliding;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Init component
listviewsliding = (ListView) findViewById(R.id.listView);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
itemSlideMenus = new ArrayList<>();
//Add itmes for sliding list
itemSlideMenus.add(new ItemSlideMenu(R.drawable.time, "Masjid Timing"));
itemSlideMenus.add(new ItemSlideMenu(R.drawable.mosque, "My Mosque"));
itemSlideMenus.add(new ItemSlideMenu(R.drawable.settings, "Setting"));
slidingMenuAdapter = new SlidingMenuAdapter(this, itemSlideMenus);
listviewsliding.setAdapter(slidingMenuAdapter);

//Display icon to open/ close sliding list
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

//Set title
setTitle(itemSlideMenus.get(0).getTitle());
//Item selected
listviewsliding.setItemChecked(0, true);
//Close menu
drawerLayout.closeDrawer(listviewsliding);

//Display fragment 1 when start
replaceFragment(0);

//Handle on item click
listviewsliding.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Set title
setTitle(itemSlideMenus.get(position).getTitle());
//Item selected
listviewsliding.setItemChecked(position, true);
//Replce Fragment
replaceFragment(position);
//close menu
drawerLayout.closeDrawer(listviewsliding);
}
});

actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_opened, R.string.drawer_closed){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}

@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu();
}
};

drawerLayout.setDrawerListener(actionBarDrawerToggle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}

//Create method replace fragment

private void replaceFragment (int pos){
Fragment fragment = null;
switch (pos){
case 0:
fragment = new MasjidTiming();
break;
case 1:
fragment = new MyMosque();
break;

case 2:
fragment = new Settings();
break;
default:
fragment = new MasjidTiming();
break;
}
if (null!=fragment)
{
FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_consent, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
}


Adapter class is:

public class SlidingMenuAdapter extends BaseAdapter {
private Context context;
private List<ItemSlideMenu> Istitem;

public SlidingMenuAdapter(FindNearbyMosque context, List<ItemSlideMenu> istitem) {
this.context = context;
Istitem = istitem;
}


public int getCount() {
return Istitem.size();
}

@Override
public Object getItem(int position) {
return Istitem.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View v=View.inflate(context, R.layout.itme_sliding_menu,null);
ImageView img = (ImageView)v.findViewById(R.id.item_img);
TextView tv=(TextView)v.findViewById(R.id.item_title);

ItemSlideMenu itemSlideMenu=Istitem.get(position);
img.setImageResource(itemSlideMenu.getImgid());
tv.setText(itemSlideMenu.getTitle());
return v;
}
}

Answer

You didn't called setContentView in your activity which makes it impossible to find your ListView.