In-app connections

1

I am trying to make my application make an emergency call and call a pre-defined emergency number, but when I open Activity it simply to have already looked and refiled the code 3 times, but the error persists, it presents the following error:

Stack trace:

  

FATAL EXCEPTION: main                                                                                     Process: com.example.matheus.privatewalletm, PID: 3321                                                                                     java.lang.IllegalStateException: Could not find method link (View)   in a parent or ancestor Context for android: onClick attribute defined   on view class android.support.v7.widget.AppCompatButton with id   push button                                                                                         at   android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.resolveMethod (AppCompatViewInflater.java:327)                                                                                         at   android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick (AppCompatViewInflater.java:284)                                                                                         at android.view.View.performClick (View.java:4780)                                                                                         at android.view.View $ PerformClick.run (View.java:19866)                                                                                         at android.os.Handler.handleCallback (Handler.java:739)                                                                                         at android.os.Handler.dispatchMessage (Handler.java:95)                                                                                         at android.os.Looper.loop (Looper.java:135)                                                                                         at android.app.ActivityThread.main (ActivityThread.java:5254)                                                                                         at java.lang.reflect.Method.invoke (Native Method)                                                                                         at java.lang.reflect.Method.invoke (Method.java:372)                                                                                         at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:903)                                                                                         at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:698)

    public class Ligar extends AppCompatActivity implements View.OnClickListener {
private Button buttonligar;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ligacao);

        buttonligar = (Button) findViewById(R.id.buttonligar);
    }

    public void onClick(View view) {


        String telefone = "190";
        try {
            Uri uri = Uri.parse("tel:" + telefone);
            Intent intent = new Intent(Intent.ACTION_CALL, uri);

            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            startActivity(intent);
    }
    catch (Exception e)
    {
        AlertDialog.Builder dlg = new AlertDialog.Builder(this);
        dlg.setMessage(getResources().getString(R.string._menssagem));
        dlg.show();

        }


    }
}

XML

    

    <Button
        android:id="@+id/buttonligar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ligar Emergencia"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="OnClick"/>

</RelativeLayout>
    
asked by anonymous 11.09.2016 / 07:07

1 answer

1

First you need to address the issues of your button by entering setOnClickListener() this way:

buttonligar = (Button) findViewById(R.id.buttonligar);
buttonligar.setOnClickListener(this);

Then you can create a role for permissions-related handling

public boolean getPermissionCall(Context context) {
        int REQUEST_PERMISSION_CALL = 221;
        boolean res = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                res = false;
                ActivityCompat.requestPermissions((Activity) context, new String[]{
                                Manifest.permission.CALL_PHONE},
                        REQUEST_PERMISSION_CALL);

            }
        }
        return res;
    }

So every time you connect, you first check your permission to CALL_PHONE . Your onClick will look like this:

public void onClick(View view) {

        if(getPermissionCall(this)){
            String telefone = "190";
            try {
                Uri uri = Uri.parse("tel:" + telefone);
                Intent intent = new Intent(Intent.ACTION_CALL, uri);

                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    return;
                }
                startActivity(intent);
            } catch (Exception e) {
                AlertDialog.Builder dlg = new AlertDialog.Builder(this);
                dlg.setMessage("teste");
                dlg.show();

            }
        }
    }

Good luck!

    
11.09.2016 / 16:54