-2

This is my fragment class. I have a manager class with dummy data, and an adpater class thats extends the Base Adapter. There is an error in my fragment

(questionsAdapter = new QuestionsAdapter(this, questionsManager.getAllQuestions());) 

and i don't seem to understand the error. Because i have passed the right parameters and arguments in the questtionsAdapter constructor

       package sandwich.playmaker.com.afterschool.fragments;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

import sandwich.playmaker.com.afterschool.R;
import sandwich.playmaker.com.afterschool.adapters.QuestionsAdapter;
import sandwich.playmaker.com.afterschool.manager.QuestionsManager;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link QuestionsFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link QuestionsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class QuestionsFragment extends Fragment {
    private ListView lvQuestions;
    private EditText etSearchQuestion;
    private Spinner spImportance;
    private QuestionsAdapter questionsAdapter;
    private QuestionsManager questionsManager;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


}

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment QuestionsFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static QuestionsFragment newInstance(String param1, String param2) {
        QuestionsFragment fragment = new QuestionsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public QuestionsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View v = inflater.inflate(R.layout.fragment_questions, container, false);

        etSearchQuestion = (EditText) v.findViewById(R.id.etSearchQuestion);
        spImportance = (Spinner) v.findViewById(R.id.spImportance);
        lvQuestions = (ListView) v.findViewById(R.id.lvQuestions);

        questionsManager =new QuestionsManager();
       questionsAdapter = new QuestionsAdapter(this, questionsManager.getAllQuestions());
        lvQuestions.setAdapter(questionsAdapter);

        return v;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

}

This is my manager class

   public class QuestionsManager {

private ArrayList<QuestionsModel>allQuestions;


public QuestionsManager() {
    this.allQuestions = allQuestions;
}

public ArrayList<QuestionsModel>getAllQuestions(){
    allQuestions.add(new QuestionsModel(R.drawable.monarki, "How to eat Banku", "Food",
            "9:00pm"));
    allQuestions.add(new QuestionsModel(R.drawable.monarki, "How to eat Kenkey", "Food",
            "9:00pm"));
    allQuestions.add(new QuestionsModel(R.drawable.monarki, "How to eat Rice", "Food",
            "10:00pm"));
    allQuestions.add(new QuestionsModel(R.drawable.monarki, "How to eat Kelewele", "Food",
            "11:00pm"));
    allQuestions.add(new QuestionsModel(R.drawable.monarki, "How to eat Orange", "Food",
            "12:00pm"));
    allQuestions.add(new QuestionsModel(R.drawable.monarki, "How to eat Banana", "Food",
            "13:00pm"));
    allQuestions.add(new QuestionsModel(R.drawable.monarki, "How to eat Gob3", "Food",
            "14:40pm"));

    return allQuestions;
}

}

This is my adapter class

        public class QuestionsAdapter extends BaseAdapter {
private Context ctx;
//private QuestionsModel questionsModel;
private ArrayList<QuestionsModel>allQuestions;

public QuestionsAdapter(Context ctx,  ArrayList<QuestionsModel> allQuestions) {
    this.ctx = ctx;
    this.allQuestions = allQuestions;

   // this.questionsModel = questionsModel;
}


@Override
public int getCount() {
    return allQuestions.size();
}

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

@Override
public long getItemId(int position)
{
    return allQuestions.get(position).getIvProfilePic();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    QuestionsModel questionsModel = allQuestions.get(position);
    LayoutInflater  inflater=  (LayoutInflater) ctx.getSystemService(Context
            .LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.activity_questions, null);
    ImageView ivProfilePic = (ImageView) convertView.findViewById(R.id.ivProfilePic);
    TextView tvQuestion = (TextView) convertView.findViewById(R.id.tvQuestion);
    TextView tvTag = (TextView) convertView.findViewById(R.id.tvQuestion);
    TextView tvTime= (TextView) convertView.findViewById(R.id.tvQuestion);

    ivProfilePic.setImageResource(questionsModel.getIvProfilePic());
    tvQuestion.setText(questionsModel.getTvQuestion());
    tvTag.setText(questionsModel.getTvQuestion());
    tvTime.setText(questionsModel.getTvTime());

    return convertView;
}

}

fritz-playmaker
  • 693
  • 1
  • 10
  • 15
  • Where is the error>? – Jared Burrows Aug 04 '15 at 14:42
  • 1
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) ... seriously? `public QuestionsManager() { this.allQuestions = allQuestions; }` ? what do you think it is doing? – Selvin Aug 04 '15 at 14:42
  • @ Jared (questionsAdapter = new QuestionsAdapter(this, questionsManager.getAllQuestions());) – fritz-playmaker Aug 04 '15 at 14:43
  • Maybe because the "this" argument is an fragment, can you try replacing the "this" to "getActivity()"? – Surely Aug 04 '15 at 14:47
  • Please it's still not working. Even when I replaced the this with getActivity () – fritz-playmaker Aug 04 '15 at 17:43
  • This code is marked red. So I don't even have to run it, to display the error on the logcat .........questionsAdapter = new QuestionsAdapter(this, questionsManager.getAllQuestions()); – fritz-playmaker Aug 04 '15 at 18:12
  • @Fritz_Playmaker what. does. it. say. when. you. mouse. over. it. -_- – d0nut Aug 04 '15 at 19:17
  • @Fritz_Playmaker i just updated my answer with what seems to be the new issue (despite your lack of helpful context) – d0nut Aug 04 '15 at 19:23
  • the program runs but crahes. this is in my logcat: 900-1300/? E/Watchdog﹕ !@Sync 923 – fritz-playmaker Aug 04 '15 at 21:32

1 Answers1

3

You never actually set allQuestions to an instance of anything.

public QuestionsManager() {
    this.allQuestions = allQuestions;
}

should be

 public QuestionsManager() {
    allQuestions = new ArrayList<QuestionsModel>();
}

Additionally, some advice for the future: Try reading the stacktrace.


Since you seem to have a problem with understanding how to write a question, it would also be helpful to let everyone know that there is an error on this line:

questionsAdapter = new QuestionsAdapter(this, questionsManager.getAllQuestions());

That says something along the lines of:

Incompatible types. Required: android.app.Context. Found: blah blah blah my fragment

Change the offending line to this:

questionsAdapter = new QuestionsAdapter(getActivity(), questionsManager.getAllQuestions());

A Fragment is not a Context but an Activity is.

d0nut
  • 2,835
  • 1
  • 18
  • 23