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.
– Pablo Almeida
thanks for noticing this detail... but not yet solved the problem
– Matheus Rodrigues