0

hi friends i had tried to implement list view in the custom dialog and passing data dynamically by using JSON and searched everywhere but don't got any solution i had tried everything from past 3 days and also i don't see any wrong in my code too i had set adapter correctly i am getting this error

Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

public class Cat_comment_adap extends BaseAdapter {

    String cid;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    int idddget;
    private LayoutInflater inflater;
    private List<CurrentList> catlist;
    private PopupWindow commentWindow;
    ArrayList<CurrentList> commentlist = new ArrayList<CurrentList>();

Activity activity;
    public Cat_comment_adap(Activity activity, List<CurrentList> catlist) {
        this.activity = activity;
        this.catlist = catlist;


    }

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

    @Override
    public Object getItem(int i) {
        return catlist.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.cat_row, viewGroup, false);


        NetworkImageView singleimg = (NetworkImageView) view.findViewById(R.id.singleimg);
        final ImageView agree = (ImageView) view.findViewById(R.id.agree);
        ImageView commentbox = (ImageView) view.findViewById(R.id.commentbox);
        commentbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onShowpopup(view);
                Toast.makeText(activity, "Comments Button clicked", Toast.LENGTH_SHORT).show();
            }
        });

        final CurrentList catertlist = catlist.get(i);

        singleimg.setImageUrl(catertlist.getCatimg(), imageLoader);

        idddget = catertlist.getCcids();
        SharedPreferences eveid = activity.getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
        cid = eveid.getString("userid", "");

        agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url = "http://sampletemplates.net/majority/api.php?action=addVote&question_id=" + idddget + "&user_id=" + cid + "&vote=1&source=android";
                Log.d("Vote", "http://sampletemplates.net/majority/api.php?action=addVote&question_id=" + idddget + "&user_id=" + cid + "&vote=1&source=android");
                JsonObjectRequest voting = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            String votings = response.getString("status");
                            if (votings.equals("success")) {
                                agree.setImageResource(R.drawable.agreed);
                                Toast.makeText(activity, "Voted Successfully", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(activity, "Already Voted", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
                AppController.getInstance().addToRequestQueue(voting);
            }
        });
        return view;
    }

    public void onShowpopup(View v) {
        LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popupview = layoutInflater.inflate(R.layout.current_comment_dialog, null);
        ListView commentsListView = (ListView) v.findViewById(R.id.commentsListView);
//        commentAdapter = new comment_adapter(activity, commentlist);
        WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
        commentWindow = new PopupWindow(popupview, width - 50, height - 400, true);
        commentWindow.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.comment_bg));
        commentWindow.setFocusable(true);
        commentWindow.setOutsideTouchable(true);
        commentWindow.showAtLocation(v, Gravity.BOTTOM, 0, 100);
        commentsListView.setAdapter(new comment_adapter(activity,commentlist));
        commentAdapter.notifyDataSetChanged();
    }

Adapter class

public class comment_adapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<CurrentList> commentlist;
    public comment_adapter(Activity activity, List<CurrentList> commentlist){
        this.activity = activity;
        this.commentlist = commentlist;

    }

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

    @Override
    public Object getItem(int i) {
        return commentlist.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (inflater == null)
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (view == null)
            view = inflater.inflate(R.layout.comment_row, viewGroup, false);
        TextView user_name = (TextView) view.findViewById(R.id.user_name);
        TextView posttime = (TextView) view.findViewById(R.id.posttime);
        TextView comtsdetails = (TextView) view.findViewById(R.id.comtsdetails);
        CurrentList listofcomments = commentlist.get(i);
        user_name.setText(listofcomments.getEvtusername());
        posttime.setText(listofcomments.getTimetaken());
        comtsdetails.setText(listofcomments.getEvcomment());
        return view;
    }
}
prominere
  • 79
  • 1
  • 9

2 Answers2

0
        public void onClick(View view) {
            onShowpopup(view);
            Toast.makeText(activity, "Comments Button clicked", Toast.LENGTH_SHORT).show();
        }

Here passed view is not R.layout.cat_row as you think but it's a button that was clicked.

So just use onShowpopup(self.view) and it will work :)

or change to onClick(View clickedButton)

Ioane Sharvadze
  • 1,848
  • 16
  • 31
0

here in this class Cat_comment_adap in the method onShowpopup change View popupview = layoutInflater.inflate(R.layout.current_comment_dialog, null); ListView commentsListView = (ListView) v.findViewById(R.id.commentsListView);

to

View popupview = layoutInflater.inflate(R.layout.current_comment_dialog, null); ListView commentsListView = (ListView) popupview.findViewById(R.id.commentsListView);

because your inflating listview from this layout so u have to give the name of inflated layout object there not the parameter of the method

Ayub Baba
  • 72
  • 8