1

I am creating a call application i created an adapter for Recycler view on requesting CALL_PHONE there is showing the error

E/AndroidRuntime: FATAL EXCEPTION: main Process: in.welcomeyou.merchant, PID: 14890 java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity

@Override
public void onBindViewHolder(EnquiryViewHolder holder, int position) {
    Enquiry enquiry = enquiryList.get(position);
    holder.domainTextView.setText(enquiry.getDomain());
    holder.nameTextView.setText(enquiry.getName());
    holder.commentsTextView.setText(enquiry.getComments());
    holder.dateTextView.setText(enquiry.getDate());

    holder.callBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            if (ActivityCompat.checkSelfPermission(mCtx, CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.CALL_PHONE},
                        Constants.CALL_PHONE);
                return;
            }

            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:123456"));
            mCtx.startActivity(intent);
        }

    });


}
rakeshrk.vbis
  • 33
  • 1
  • 3
  • what is the type of mCtx ? You may want to take a look at this : https://stackoverflow.com/questions/23546303/android-app-application-cannot-be-cast-to-android-app-activity – Amine Messaoudi Jun 18 '18 at 14:30

2 Answers2

1

Please write this as below

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:123456"));
((YourActivityName)mContext).startActivity(intent);
Vasant
  • 3,475
  • 3
  • 19
  • 33
1

Where you are passing context to this adapter use ActivityName.this or this . Maybe now you passed

getApplicationContext();

And you get this exception because you can not cast the Application to Activity because Application is not a sub-class of Activity.

Fereshteh Naji
  • 104
  • 1
  • 11
  • its calling from public class DashboardActivity extends AppCompatActivity { private void getEnquiries() { EnquiryAdapter adapter = new EnquiryAdapter(getApplication(), enquiryList); recyclerView.setAdapter(adapter); – rakeshrk.vbis Jun 18 '18 at 14:38
  • Just Pass your ActivityClassname.this on place getApplication. – Vasant Jun 18 '18 at 14:42
  • 1
    ok just write you adapter definition like this:EnquiryAdapter adapter = new EnquiryAdapter(DashboardActivity.this, enquiryList); – Fereshteh Naji Jun 18 '18 at 14:42