Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on autocomplete error #734

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class PlaceAutocompleteActivity : AppCompatActivity() {
/**
* Launches Autocomplete activity and handles result
*/
private var autocompleteLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
private val autocompleteLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result ->
when (result.resultCode) {
AutocompleteActivity.RESULT_OK -> {
Expand All @@ -141,8 +141,8 @@ class PlaceAutocompleteActivity : AppCompatActivity() {
}
}
AutocompleteActivity.RESULT_ERROR -> {
val status = Autocomplete.getStatusFromIntent(intent)
binding.response.text = status.statusMessage
val status = Autocomplete.getStatusFromIntent(result.data)
binding.response.text = "Error: ${status.statusMessage}"
Comment on lines +144 to +145
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has a potential NPE. How about the following change?

Suggested change
val status = Autocomplete.getStatusFromIntent(result.data)
binding.response.text = "Error: ${status.statusMessage}"
binding.response.text = result.data?.let { intent ->
getString(
R.string.error_message,
Autocomplete.getStatusFromIntent(intent).statusMessage
)
} ?: getString(R.string.error_unknown)

And add the following to demo-kotlin/app/src/main/res/values/strings.xml

<!-- message to show for errors received from the API. -->
  <string name="error_message" translatable="false">Error: %1$s</string>

  <!-- message to show if the error status is unavailable. -->
  <string name="error_unknown" translatable="false">Unexpected error</string>

}
AutocompleteActivity.RESULT_CANCELED -> {
// The user canceled the operation.
Expand Down Expand Up @@ -287,4 +287,4 @@ class PlaceAutocompleteActivity : AppCompatActivity() {
.setMessage(messageResId)
.show()
}
}
}