【问题标题】:list view adapter setting null object on custom dialog列表视图适配器在自定义对话框上设置空对象
【发布时间】:2015-11-28 18:35:35
【问题描述】:

嗨朋友们,我曾尝试在自定义对话框中实现列表视图并使用 JSON 动态传递数据并到处搜索,但没有得到任何解决方案我已经尝试了过去 3 天的所有内容,而且我没有看到任何错误在我的代码中我也正确设置了适配器我收到了这个错误

尝试在空对象引用上调用虚拟方法“void android.widget.ListView.setAdapter(android.widget.ListAdapter)”

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

适配器类

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

【问题讨论】:

  • 表示它没有任何价值
  • cmetsListView 为空
  • 是的,@noev 链接了一个类似的问题,其答案可能对您有所帮助。

标签: java android listview dialog


【解决方案1】:

这里在这个类cat_comment_adap中的方法onShowpopup改变 View popupview = layoutInflater.inflate(R.layout.current_comment_dialog, null); ListView cmetsListView = (ListView) v.findViewById(R.id.cmetsListView);

到

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

因为你从这个布局中膨胀的列表视图所以你必须给膨胀的布局对象的名称而不是方法的参数

【讨论】:

    【解决方案2】:
            public void onClick(View view) {
                onShowpopup(view);
                Toast.makeText(activity, "Comments Button clicked", Toast.LENGTH_SHORT).show();
            }
    

    这里传递的视图不是你想象的R.layout.cat_row,而是一个被点击的按钮。

    所以只需使用onShowpopup(self.view) 即可:)

    或改成onClick(View clickedButton)

    【讨论】:

    • :D我弄乱了关键字,你必须用this.view,而不是self,java中没有关键字self。
    猜你喜欢
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多