2015-11-28 32 views
0

嗨,朋友,我试图实现自定义对话框中的列表视图,并通过使用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; 
    } 
} 
+2

[什么是空引用(http://stackoverflow.com/questions/ 4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – noev

+0

表示它没有任何值 – prominere

+0

commentsListView为空 – noev

回答

0

这里在这个类Cat_comment_adap的方法onShowpopup改变 查看popupview = layoutInflater.inflate(R.layout.current_comment_dialog , 空值); ListView commentsListView =(ListView)v.findViewById(R.id.commentsListView);

查看popupview = layoutInflater.inflate(R.layout.current_comment_dialog,NULL); ListView commentsListView =(ListView)popupview.findViewById(R.id.commentsListView);

因为此布局的充气的ListView所以我只好给膨胀布局对象的名的方法不是参数

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

+0

没有改变同样的错误先生 – prominere

+0

:D我瞎关键字,你必须使用this.view,而不是自我,没有关键字自我在Java中。 –

相关问题