如何解决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``
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。