我的布局的屏幕截图
我在xml和java中添加了TranscriptMode。但它不起作用...... 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>
listview是片段的一部分,当底栏布局的索引0被点击时,我调用它。 片段类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;
}
}
将listview的布局高度更改为wrap_content后,我收到以下错误。(App崩溃)
java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.buckydroid.chat.ContactList$viewHolder.user_name' on a null object reference
答案 0 :(得分:2)
您需要设置视图持有者标记。
@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;
}
答案 1 :(得分:2)
根据我上面的评论,您需要使用setTag
和getTag
,因为如果else
案例已执行,您的holder
将为null
,因此例外,所以这样做
@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;
}
注意:您可以使用convertView
代替row
,无需创建额外的引用变量。