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>