1

Here is main activity.

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

EditText number;
Button makecall;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    number = findViewById(R.id.number);
    makecall = findViewById(R.id.makecall);
    call();
}

private void call() {
    makecall.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent;
            intent = new Intent(Intent.ACTION_DIAL);
            intent = intent.setData(Uri.parse("number:"+number.getText().toString()));
            startActivity(intent);
        }
    });
}
}

I added this permission request to manifest file.

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

Here is my XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.bimstajyer1.calisma10.MainActivity">

<EditText
    android:id="@+id/number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Number"
    android:inputType="number"
    android:maxLength="11" />

<Button
    android:id="@+id/makecall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Make a call" />
</LinearLayout>

I am this getting error when I press the call button to make call.

FATAL EXCEPTION: main

Process: com.example.bimstajyer1.calisma10, PID: 25198 android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.DIAL dat=number: }

Error line is : startActivity(intent);

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
  • why you calling your call(); method from onCreate() ???, set click listener in Button in onCreate(), validate your number and then call your call() method. – Gaurav Feb 06 '18 at 10:03

2 Answers2

1
private void call() {
makecall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
            PackageManager pm = getPackageManager();
            if (pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
               if (!number.contains("tel:"))
                    number = "tel:" + number;
                Uri link = Uri.parse(number);
                Intent callIntent = new Intent(Intent.ACTION_DIAL, link);
                startActivity(callIntent);
            }else{
                Toast.makeText(this,"No call facility available in device!",Toast.LENGTH_SHORT).show();

}

}); }

Now skip all crashes.

Gautam Kushwaha
  • 281
  • 3
  • 15
0

Please look into this

private void call() {
makecall.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent;
        intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:"+number.getText().toString()));
        startActivity(intent);
    }
});
}
Akash
  • 961
  • 6
  • 15