Capture d'écran de ma mise en page

Layout ss

J'ai ajouté TranscriptMode en xml et java. Mais ça ne marche pas ... Xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/contact_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.buckydroid.chat.ContactList">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/contact_lists"
        android:transcriptMode="alwaysScroll"
        android:layout_weight="1"/>
</RelativeLayout>

La vue de liste fait partie du fragment que je l'appelle lorsque l'index 0 de la mise en page bottombar est cliqué. Classe de fragment Code Java:

public class ContactList extends Fragment implements AdapterView.OnItemClickListener{
    private ListView contactList;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.contact_list,container,false) ;
        contactList = (ListView)view.findViewById(R.id.contact_lists);
        contactList.setAdapter(new ContactListAdapter(getActivity()));

        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    class RowData {
        String user_name;
        int user_img;
        String user_msg;
        int message_status;

        public RowData(String user_name, int user_img, String user_msg, int message_status) {
            this.user_name = user_name;
            this.user_img = user_img;
            this.user_msg = user_msg;
            this.message_status = message_status;
        }
    }
    class viewHolder{
        CircleImageView user_img;
        ImageView message_stats;
        TextView user_name;
        TextView user_message;
        viewHolder(View v){
            user_img = (CircleImageView)v.findViewById(R.id.user_img_list);
            user_name = (TextView)v.findViewById(R.id.contact_name);
            user_message = (TextView)v.findViewById(R.id.user_message);
            message_stats = (ImageView)v.findViewById(R.id.message_status);

        }
    }
    class ContactListAdapter extends BaseAdapter{

        ArrayList<RowData> list;
        Context context;
        ContactListAdapter(Context c){
            context = c;
            list = new ArrayList<RowData>();
            String[] username = {"Bucky","Marie","Melvin","Rheo","Emi","Lighter","Toco","Ravi","krushna"};
            String[] message = {"Bunch Of Shitty Texts . ","Bunch Of Shitty Texts . ","Bunch Of Shitty Texts . ","Bunch Of Shitty Texts . ","Bunch Of Shitty Texts .","Bunch Of Shitty Texts . ","Bunch Of Shitty Texts . u","Bunch Of Shitty Texts . Fuck You","Bunch Of Shitty Texts"};
            int[] user_image = {R.drawable.man,R.drawable.man,R.drawable.man,R.drawable.man,R.drawable.man,R.drawable.man,R.drawable.man,R.drawable.man,R.drawable.man};
            int[] message_stats = {R.drawable.unreadmessage,R.drawable.unreadmessage,R.drawable.unreadmessage,R.drawable.unreadmessage,R.drawable.unreadmessage,R.drawable.unreadmessage,R.drawable.unreadmessage,R.drawable.unreadmessage,R.drawable.unreadmessage};
            for (int i = 0; i <9; i++){
                list.add(new RowData(username[i],user_image[i],message[i],message_stats[i]));
            }
        }

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            viewHolder holder = null;
            if (row==null){
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.contact_list_style,parent,false);
                holder = new viewHolder(row);
            }else{
                Toast.makeText(context, "GRRRRRRRRRRR", Toast.LENGTH_SHORT).show();
            }
            RowData temp = list.get(position);
            holder.user_name.setText(temp.user_name);
            holder.message_stats.setImageResource(temp.message_status);
            holder.user_img.setImageResource(temp.user_img);
            holder.user_message.setText(temp.user_msg);
            return row;
        }
    }

Après avoir changé la hauteur de mise en page de listview en wrap_content, j'obtiens l'erreur suivante.

java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.buckydroid.chat.ContactList$viewHolder.user_name' on a null object reference
1
Doge 16 janv. 2017 à 19:57

2 réponses

Meilleure réponse

Conformément à mon commentaire ci-dessus, vous devez utiliser setTag et getTag car si else cas est exécuté, votre holder sera null, d'où l'exception, donc fais ça

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    viewHolder holder = null;
    if (row==null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.contact_list_style,parent,false);
        holder = new viewHolder(row);
        row.setTag(holder);
        // ^^^ set the tag
    }else{
        Toast.makeText(context, "GRRRRRRRRRRR", Toast.LENGTH_SHORT).show();
        holder=(viewHolder)row.getTag();
        // do this , initialize your holder otherwise holder stays null
    }
    RowData temp = list.get(position);
    holder.user_name.setText(temp.user_name);
    holder.message_stats.setImageResource(temp.message_status);
    holder.user_img.setImageResource(temp.user_img);
    holder.user_message.setText(temp.user_msg);
    return row;
}

Remarque: vous pouvez utiliser convertView au lieu de row, pas besoin de créer une variable de référence supplémentaire.

2
Pavneet_Singh 16 janv. 2017 à 18:04

Vous devez définir l'étiquette de support de vue.

   @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View row = convertView;
                viewHolder holder = null;
                if (row==null){
                    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    row = inflater.inflate(R.layout.contact_list_style,parent,false);
                    holder = new viewHolder(row);
                    row .setTag(holder);
                }else{
                    holder = (Holder) row.getTag();
                    Toast.makeText(context, "GRRRRRRRRRRR", Toast.LENGTH_SHORT).show();
                }
                RowData temp = list.get(position);
                holder.user_name.setText(temp.user_name);
                holder.message_stats.setImageResource(temp.message_status);
                holder.user_img.setImageResource(temp.user_img);
                holder.user_message.setText(temp.user_msg);
                return row;
            }
2
santosh kumar 16 janv. 2017 à 17:45