0

i m getting strange behavior. when i m accessing my list in QuestionMenuAdapter. I have four classes.

1.TestActivity 2.TestActivityDrawerFragment 3.QuestionMenuAdapter 4.QuestionFetch

what i m doing is that... i m creating the object of QuestionFetch class in my TestActivity class. then i m passing the list of Question (that i m getting from QuestionFetch class) to TestActivityDrawerFragment via setUp method, and here list obtain form TestActivity is passing to QuestionMenuAdapter.

now in QuestionMenuAdapter ,in the onBindViewHolder() i m getting my question one by one .... but i got NPE

below is my Code QuestionFetch Class

public class QuestionFetch {
List<Question> questionList=new ArrayList<>();

public List<Question> getQuestionList() {
    return questionList;
}


public QuestionFetch()
{

    for(int i=0;i<15;++i)
    {

        String option[]=new String[4];
        Question item=new Question();
        item.setQuestion("What is my name"+i);
        item.setCorrectOptionIndex(i%4);
        item.setNegativeMarks("1/2");
        item.setSolution("deepak is name set by mom"+i);
        item.setTypeOfQuestion("four");
        for(int j=0;j<4;++j)
            option[j]="deepak"+(i+j);
        item.setOptions(option);
        questionList.add(item);

    }
}

}

now in TestActivity i m creating the object of QuestionFetch Class

questionFetch=new QuestionFetch();
    drawerFragment=(TestActivityDrawerFragment) getSupportFragmentManager()
            .findFragmentById(R.id.test_navigation_drawer_fragment);
     // here passing the list of Questions to setUp method of TestActivityDrawerFragment
    drawerFragment.setUp((DrawerLayout) findViewById(R.id.test_drawer_layout),questionFetch.getQuestionList());

now in TestActivityDrawerFragment class

public void setUp( DrawerLayout drawerLayout,List<Question> data) {

    this.data=data;}

and in onCreateView passing this list to QuestionMenuAdapter

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    data=new QuestionFetch().getQuestionList();
    // here is list is in data
    adapter=new QuestionMenuAdapter(getActivity(),data);
    adapter.setOnClickListener(this);
    return inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
}

and finally i m accessing my list in onBindViewHolder() in QuestionMenuAdapter class

public class QuestionMenuAdapter extends RecyclerView.Adapter<QuestionMenuAdapter.PageViewHolder>{



Context context;
boolean addOrRemoveItemClicked=false;
private LayoutInflater inflater;
ClickListener clickListener;
List<Question> data= Collections.emptyList();

public QuestionMenuAdapter(Context context, List<Question> data)
{
    inflater = LayoutInflater.from(context);

    this.context=context;
    this.data=data;
    //here i have checked that my question is getting but not in onBindVievHolder
   Toast.makeTosat(context,""+this.data.get(0).getQuestion,Toast.LENGTH_SHORT).show();


}

@Override
public void onBindViewHolder(QuestionMenuAdapter.PageViewHolder holder, int position) {

    holder.customTextView.setText(""+(position+1));
   // here i m getting NPE
    holder.questionText.setText(data.get(position).getQuestion());
}

0 Answers0