NullpointerException when setting adapter for listview in dialog

前端 未结 3 1374
醉梦人生
醉梦人生 2021-01-26 08:00

I want to display a custom dialog that have a listview inside it. First take a look on my code below.

Dialog:

protected void onPostExecute(String file_ur         


        
相关标签:
3条回答
  • 2021-01-26 08:16

    Inflate your view and use the object returned by the inflater to look for the ListView inside the layout

    View view = inflater.inflate(R.layout.dialog_add, null)
    ListView lv = (ListView) view.findViewById(R.id.lvAddDialog); 
    ListviewContactAdapter adapter = new ListviewContactAdapter(getActivity(), listContact);
    lv.setAdapter(adapter);             
    builder.setView(view);
    
    0 讨论(0)
  • 2021-01-26 08:17

    Change this:

    ListviewContactAdapter adapter = new ListviewContactAdapter(getActivity(), listContact);
    

    to this:

    ListviewContactAdapter adapter = new ListviewContactAdapter(YourCLassName.this, listContact);
    

    OR, may be you didn't initialize listContact

    0 讨论(0)
  • 2021-01-26 08:17

    When you get the ListView yet not set te layout in the Dialog. You need build first your dialog and after get the ListView

    Check this:

    protected void onPostExecute(String file_url) {
        btnInvite.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
    
                LayoutInflater inflater = getActivity().getLayoutInflater();
    
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    
    
                builder.setView(inflater.inflate(R.layout.dialog_add, null))
                .setTitle("Invite people")                  
                .setNegativeButton("Cancel", new OnClickListener() {
    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.cancel();
                    }
                });                                 
                Dialog dialog = builder.create();
    
                ListView lv = (ListView) dialog.findViewById(R.id.lvAddDialog); 
                ListviewContactAdapter adapter = new ListviewContactAdapter(getActivity(), listContact);
                lv.setAdapter(adapter);     
                dialog.show();                  
                }
        });
     }
    

    }

    0 讨论(0)
提交回复
热议问题