Links within the application

Asked

Viewed 120 times

1

I’m trying to get my app to make an emergency call and call a pre-defined emergency number, but when I open Activity she simply for already looked and redid 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 buttonligar 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>
  • you wrote "Onclick" instead of "onClick" in XML. This could be it.

  • thanks for noticing this detail... but not yet solved the problem

1 answer

1

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

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

Then you can create a function for the treatment related permissions

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 make the call, you first check your permission to CALL_PHONE. Your onClick will remain so:

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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.