1

I have introduced a listview inside a drawerlayout in my app. I have created customadapter and all the required stuff, but I am getting the following error:

java.lang.RunTimeException:UnabletoStart activity ComponentInfo{com.example.myapp/com.example.myapp.SecondActivity; java.lang.NullPointerException: Attempt to invoke virtual method 'void (android.widget.ListView)' on a null object reference at android.app.activity.launchActivity(ActivityThread.java:2843)

I hope this much helps (I have to type the error). I am new to android(esp. adapters and listview) so please help me in finding the bug.

CustomAdapter.java


package com.example.myapp;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class CustomAdapter extends ArrayAdapter<TrueFalse> {
    Context mContext;
    int mlayoutResourceId;
    TrueFalse[] questions;
    public CustomAdapter(Context mContext,int layoutResourceId,TrueFalse[] questions)
    {
        super(mContext,layoutResourceId,questions);
        this.mContext=mContext;
        this.questions=questions;
        this.mlayoutResourceId=layoutResourceId;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {

            convertView=((Activity)mContext).getLayoutInflater().inflate     (mlayoutResourceId,parent,false);




        TextView quesno=(TextView)convertView.findViewById(R.id.quesnameid);
        TrueFalse mQ=getItem(position);
        int index=mQ.getIndex();
        quesno.setText("Question No."+Integer.toString(index));
        return convertView;
    }

}

SecondActivity.java

public class SecondActivity extends AppCompatActivity implements
        NavigationView.OnNavigationItemSelectedListener{


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

        setContentView(R.layout.drawerlayout);


        mDrawerList=(ListView)findViewById(R.id.left_drawer);
        CustomAdapter adapter=new CustomAdapter(this,R.layout.listview_item,mQuestionBank);
        mDrawerList.setAdapter(adapter);
}
#I think this much code is sufficient

}

listview_layout.xml

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/content_frame"/>
    <ListView
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:id="@+id/left_drawer"
        android:layout_gravity="end"
        android:choiceMode="singleChoice"
        android:divider="@color/colorPrimary"

        />

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

nav_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    tools:openDrawer="start"
    android:background="#ffff99"
    android:fitsSystemWindows="true">

    <include
        layout="@layout/activity_main"

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    <include
        layout="@layout/listview_layout"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

    <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_main"
        app:menu="@menu/activity_main_drawer"


        />


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

Thanks in advance.

Bully Maguire
  • 211
  • 3
  • 15

4 Answers4

0

You should initialize the ListView before you use it.

John Smith
  • 33
  • 7
0

You are missing setContentView(R.layout.second_activity); // or whatever you called it in SecondActivity so no layout is ever inflated and that is why ListView can not be found. So just below

        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
Yupi
  • 4,402
  • 3
  • 18
  • 37
  • I actually missed it in code. I have now added all the xml files as well as this line. Sorry for incovenience – Bully Maguire May 15 '19 at 14:59
  • `setContentView(R.id.drawerlayout);` - is not correct. You have to provide layout not `id` of particular `View`. So in your scenario `setContenView` method must accept layout. – Yupi May 15 '19 at 15:02
  • Actually I meant the same. The basic code is expected to be correct. I expect the error in listview/adapter part as the error says – Bully Maguire May 15 '19 at 15:04
  • Hey, can you help me? Now, all my drawers are working fine, but the activity has become unresponsive. (eg, the buttons in the SecondActivity.java don't work) Can you suggest something? @Yupi – Bully Maguire May 17 '19 at 13:51
0

I also once had the same problem. I don't know about you but as you said about having two drawers, I forgot to include drawer in activity code. It caused similar error. Check if you have done that.

Tojra
  • 673
  • 5
  • 20
0

Refering to Yupi's answer:

  1. setContentView() should not target layout id but its file name.

  2. it returns java.lang.NullPointerException because it cant find listViews of id R.id.left_drawer since you put the wrong target layout in the setContentView()

sorry for my bad english.

Alvin Tom
  • 119
  • 4