ListView 中的 ImageButton onclick 侦听器不起作用点击后应用程序崩溃

问题描述

我编写了以下代码来从自定义 listView 内部进行调用,但是每当我按下 Imagebutton 时,应用程序就会崩溃。我尝试了不同的文章和教程,但无法实现。有人可以看到我的代码并建议如何操作吗?

模型列表视图的 XML

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EADEF1">
<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/photo"
    android:layout_width="40dp"
    android:layout_height="50dp"
    android:layout_margin="5dp"
    android:layout_gravity="center"
    android:src="@drawable/logobsca">
</de.hdodenhof.circleimageview.CircleImageView>

<TextView
    android:id="@+id/textName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Name"
    android:layout_toRightOf="@id/photo"
    android:textSize="14sp"
    android:textColor="#4527A0"
    android:textStyle="bold"/>
<TextView
    android:id="@+id/textDesignation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/textName"
    android:layout_toRightOf="@id/photo"
    android:text="Designation"
    android:textSize="12sp"
    android:textColor="#1F2020"
    android:textStyle="normal"/>
  <TextView
    android:id="@+id/textPhone"
    android:layout_below="@id/textDesignation"
    android:layout_toRightOf="@id/photo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Phone"
    android:clickable="true"
    android:textColor="#1F2020"
    android:textSize="12sp"
    android:textStyle="normal" />
    

     <ImageButton
         android:id="@+id/callButton"
         android:layout_width="40dp"
         android:layout_height="40dp"
         android:layout_alignParentTop="true"
         android:clickable="false"
         android:layout_alignParentEnd="true"
         android:layout_marginTop="9dp"
         android:layout_marginEnd="13dp"
         android:background="@drawable/roundcall"
         android:src="@android:drawable/ic_menu_call" />
</RelativeLayout>

模拟课堂活动

    ```public class ListMobileModel extends AppCompatActivity {
String Name;
String Designation;
String Phone;
ImageButton CallButton;
int Photo;

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public String getDesignation() {
    return Designation;
}

public void setDesignation(String designation) {
    Designation = designation;
}

public String getPhone() {
    return Phone;
}

public void setPhone(String phone) {
    Phone = phone;
}

public int getPhoto() {
    return Photo;
}

public void setPhoto(int photo) {
    Photo = photo;
}

public ListMobileModel(String name,String designation,String phone,int photo) {
    Name = name;
    Designation = designation;
    Phone = phone;
    Photo = photo;
}

}```

ListView 适配器

    public class ListViewAdapter extends BaseAdapter {

Activity context;
LayoutInflater inflater;
List<ListMobileModel> mobilelist;
ArrayList<ListMobileModel> arrayList;

public ListViewAdapter( Activity context,List<ListMobileModel> mobilelist) {

    context = context;
    this.mobilelist=mobilelist;
    inflater=LayoutInflater.from(context);
    this.arrayList=new ArrayList<ListMobileModel>();
    this.arrayList.addAll(mobilelist);

}
public class ViewHolder{
    TextView nameTV,designationTV,mobileTV;
    ImageView photoTV;
    ImageButton callButton;

}

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

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

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

public View getView(int position,View view,ViewGroup parent){
    ViewHolder holder;
    if (view==null){
        holder=new ViewHolder();
        view=inflater.inflate(R.layout.activity_listmobile_model,null);


        // locate the views in activity_listmobile_model.xml)

        holder.nameTV=view.findViewById(R.id.textName);
        holder.designationTV=view.findViewById(R.id.textDesignation);
        holder.mobileTV=view.findViewById(R.id.textPhone);
        holder.photoTV=view.findViewById(R.id.photo);
        holder.callButton=view.findViewById(R.id.callButton);

        view.setTag(holder);
    }
        else{
            holder = (ViewHolder) view.getTag();
    }
        //set the results into Textview
        holder.nameTV.setText(mobilelist.get(position).getName());
        holder.designationTV.setText(mobilelist.get(position).getDesignation());
        holder.mobileTV.setText(mobilelist.get(position).getPhone());
        holder.photoTV.setimageResource(mobilelist.get(position).getPhoto());

        ImageButton callButton;
        callButton=view.findViewById(R.id.callButton);



        callButton.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String cellno=mobilelist.get(position).getPhone();
                Intent callIntent =new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:" + cellno));
                if(ActivityCompat.checkSelfPermission(context,Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
                    return;
                }
                context.startActivity(callIntent);
            }
        });
        return view;
}
//Filter
public void filter(String charText){
    charText=charText.toLowerCase(Locale.getDefault());
    mobilelist.clear();
    if(charText.length()==0){
        mobilelist.addAll(arrayList);
    }
    else{
        for(ListMobileModel row:arrayList){
            if (row.getName().toLowerCase(Locale.getDefault()).contains(charText)|| (row.getDesignation().toLowerCase(Locale.getDefault()).contains(charText))){
                mobilelist.add(row);
            }

        }
    }
    notifyDataSetChanged();
}

}```

列表视图活动

    ```public class ListViewAdapter extends BaseAdapter {

Activity context;
LayoutInflater inflater;
List<ListMobileModel> mobilelist;
ArrayList<ListMobileModel> arrayList;

public ListViewAdapter( Activity context,Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
                    return;
                }
                context.startActivity(callIntent);
            }
        });
        return view;
}
//Filter
public void filter(String charText){
    charText=charText.toLowerCase(Locale.getDefault());
    mobilelist.clear();
    if(charText.length()==0){
        mobilelist.addAll(arrayList);
    }
    else{
        for(ListMobileModel row:arrayList){
            if (row.getName().toLowerCase(Locale.getDefault()).contains(charText)|| (row.getDesignation().toLowerCase(Locale.getDefault()).contains(charText))){
                mobilelist.add(row);
            }

        }
    }
    notifyDataSetChanged();
}

}``` enter image description here``

解决方法

使用 RecyclerView 而不是 ListView

RecyclerView 的 Adapter 和 ListView 的 Adapter 也是一样的。

Click 点击此处了解更多详情。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...