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

        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

}

Thanks in advance.

Related posts

Recent Viewed