I wrote following codes to make a call from inside the custom listView but whenever I press the Imagebutton the App crashes. I tried different articles and tutorials but could not make it happen. Could anybody see my codes and suggest how to do it?
Xml of model listview
<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>
Model Class Activity
```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 Adapter
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();
}
}```
List View Activity
```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();
}
}``` enter image description here``