Issue
I was working yesterday and not today. Have two buttons one to go back, one that is an emergency button that calls a method to open the dialer for a call to the ambulance. I think it may be an XML problem not sure was working fine now unresponsive maybe I changed something I shouldn't have. When it was clicking earlier it was saying the method could not be found which I just don't get it as it is in the correct file
ReportActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//allows for a smoother transition
overridePendingTransition(0, 0);
setContentView(R.layout.activity_report);
EditText incidentReport = this.findViewById(R.id.Incident_Report);
incidentReport.setSelection(0);
try {
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ReportActivity.this, MainActivity.class);
startActivity(intent);
}
});
} catch (NullPointerException n) {
Toast.makeText(this, "Error,", Toast.LENGTH_LONG);
}
try {
emergency.setOnClickListener(new View.OnClickListener() {
public void onClick(@NonNull View v) {
Intent call = new Intent(ReportActivity.this, CallActivity.class);
startActivity(call);
}
});
} catch (NullPointerException n) {
Toast.makeText(this, "Error,", Toast.LENGTH_LONG);
}
}
@Override
public void onClick(View v) {
}
private boolean checkPermission(String permission) {
int permissionCheck = ContextCompat.checkSelfPermission(this, permission);
return (permissionCheck == PackageManager.PERMISSION_GRANTED);
}
public void call(View view) {
if (checkPermission("android.permission.CALL_PHONE")) {
Intent call = new Intent(Intent.ACTION_DIAL);
String number = "tel:" + getString(R.string.phone_number);
call.setData(Uri.parse(number));
startActivity(call);
}
}
}
activity_report.xml
android:gravity="start"
android:hint="@string/Hint"
android:inputType="text"
android:textAlignment="textStart"
android:textCursorDrawable="@drawable/color_cursor"
app:layout_constraintBottom_toTopOf="@+id/submit_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/submit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="32dp"
android:background="@drawable/yes_button"
android:text="Submit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Incident_Report" />
<Button
android:id="@+id/back_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
android:background="@drawable/diagnose_button"
android:onClick="onClick"
android:text="Back"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Incident_Report" />
<Button
android:id="@+id/emergency_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="131dp"
android:layout_marginBottom="106dp"
android:background="@drawable/no_button"
android:onClick="call"
android:text="EMERGENCY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/submit_btn" />
</androidx.constraintlayout.widget.ConstraintLayout>
Solution
If you plan on using android:onClick="call"
in your xml then you should also have a corresponding function called call
in your activity and the logic to perform the click goes inside it.
If you plan on extending the View.onClickListener
then inside your override void onClick()
you can have a switch statement of your button ids and perform your logic
alternatively you can ignore the first two and get the id of the button and implement the click logic inside the onCreate()
Answered By - Evans Chepsiror
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.