Search code examples
androidlistviewcustom-adapter

How to make a call on button click in a ListView?


I have done the following code inside of my getView() method of CustomAdapter class. If I try to simply show a Toast, Toast appears, but if i do the following code to make a call, application crashes. What i am doing wrong here?

                call.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:" + phone.getText().toString()));
                    context.startActivity(callIntent);
                }
            });

and the stacktrace shows this error :

AndroidRuntime: FATAL EXCEPTION: main Process: com.example.hammad.contactme, PID: 26713 android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.

Solution

  • Try adding the correct permission for making call in AndroidManifest

    <uses-permission android:name="android.permission.CALL_PHONE" />
    

    and if you are running on android 6.0 or above you should also get this permission on runtime

    getting runtime permission