列表视图不会显示在我的活动中

时间:2017-10-06 21:15:53

标签: java android listview

我对android不是很有经验,我在MainActivity的布局中创建了一个listView,并在我的列表中的每一行(布局名称= view_of_list)创建了一个带有两个文本视图和ImageView的布局,并创建了MyContact类包括三个成员:String name,String phone,int Id。 我还创建了CustomAdapter类。最后我将CostumAdapter设置为listView。 当我运行代码并崩溃时; Android Monitor在CustomAdapter.java中说:

rowView = inflater.inflate(R.layout.view_of_list, parent, false);

得到了这个错误:

尝试在空对象引用上调用虚拟方法'android.view.View android.view.LayoutInflater.inflate(int,android.view.ViewGroup,boolean)'

我的错误在哪里?

activity_main.xml中

<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.list.steve.list.MainActivity">

<ListView
    android:layout_width="368dp"
    android:layout_height="495dp"
    tools:layout_editor_absoluteY="8dp"
    tools:layout_editor_absoluteX="8dp"
    android:id="@+id/list_view"/>
</android.widget.RelativeLayout>

view_of_list.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relative_layout">

<ImageView
    android:id="@+id/img_profile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@mipmap/ic_launcher"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="8dp" />
<TextView
    android:id="@+id/tv_phone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:layout_alignTop="@id/img_profile"
    android:layout_toRightOf="@id/img_profile"
    android:layout_marginLeft="8dp"/>

<TextView
    android:id="@+id/tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:layout_below="@id/tv_phone"
    android:layout_toRightOf="@id/img_profile"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="8dp"/>
</RelativeLayout>

MyContact.java

public class Mycontact {
    private String name;
    private String phone;
    private int id;
    public Mycontact(String name, String phone, int id){
        this.name = name;
        this.phone = phone;
        this.id = id;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setPhone(String phone){
        this.phone = phone;
    }
    public void setId(int id){
        this.id = id;
    }
    public String getName(){
        return name;
    }
    public String getPhone(){
        return phone;
    }
    public int getId(){
        return id;
    }

CustomAdapter.java

public class CustomAdapter extends ArrayAdapter {
private ArrayList mycontacts;
private Mycontact contact;
private LayoutInflater inflater;
private ViewHolder holder;
public CustomAdapter(Context context, ArrayList contact ) {
    super(context, R.layout.view_of_list, contact);
    this.mycontacts = contact;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View rowView = convertView;
    if(convertView == null) {
        rowView = inflater.inflate(R.layout.view_of_list, parent, false);
        holder.tv_name = (TextView) rowView.findViewById(R.id.tv_name);
        holder.tv_phone = (TextView) rowView.findViewById(R.id.tv_phone);
        holder.img_profile = (ImageView) rowView.findViewById(R.id.img_profile)
        rowView.setTag(holder);
    }else{
        holder = (ViewHolder) rowView.getTag();
    }
    contact = (Mycontact)mycontacts.get(position);
    holder.tv_name.setText(contact.getName());
    holder.tv_phone.setText(contact.getPhone());
    holder.img_profile.setImageResource(contact.getId());
    return rowView;
}

public class ViewHolder{
    TextView tv_name;
    TextView tv_phone;
    ImageView img_profile;

}

}

MainActivity.java

public class MainActivity extends AppCompatActivity {
Mycontact contact;
ArrayList<Mycontact> mycontacts;
ListView listView;
CustomAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mycontacts = new ArrayList<Mycontact>();
    adapter = new CustomAdapter(this,mycontacts);
    listView = (ListView) findViewById(R.id.list_view);
    listView.setAdapter(adapter);

}
public void AddContact(){
    mycontacts.add(new Mycontact("steve","09012345678",R.drawable.one));
    mycontacts.add(new Mycontact("hasan","091112345678",R.drawable.two));
    mycontacts.add(new Mycontact("hosein", "09151338089",R.drawable.three));
}

}

1 个答案:

答案 0 :(得分:0)

在列表上设置适配器之前,应该调用AddContact()方法。并在构造函数中初始化您的inflater,如下所示

public CustomAdapter(Context context, ArrayList contact ) {
    super(context, R.layout.view_of_list, contact);
    this.mycontacts = contact;
    this.inflater = LayoutInflater.from(context);

}

你也应该像这样

在getView()方法中构造viewHolder类
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View rowView = convertView;
    holder = new ViewHolder();
    if(convertView == null) {
        rowView = inflater.inflate(R.layout.view_of_list, parent, false);
        holder.tv_name = (TextView) rowView.findViewById(R.id.tv_name);
        holder.tv_phone = (TextView) rowView.findViewById(R.id.tv_phone);
        holder.img_profile = (ImageView) rowView.findViewById(R.id.img_profile)
        rowView.setTag(holder);
    }else{
        holder = (ViewHolder) rowView.getTag();
    }
    contact = (Mycontact)mycontacts.get(position);
    holder.tv_name.setText(contact.getName());
    holder.tv_phone.setText(contact.getPhone());
    holder.img_profile.setImageResource(contact.getId());
    return rowView;
}