I am trying to insert the address fetched from the geocoder
API, into my edittext
field. After granting the location permission in my app, no text gets inserted in my edittext
field, and even the SMS permission isn't called too.
My code:
public class message extends AppCompatActivity { EditText edittext; Button send; FusedLocationProviderClient fusedLocationProviderClient; ArrayList<String> contacts=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_message); send = findViewById(R.id.button); edittext = findViewById(R.id.msg); fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); contacts = getIntent().getStringArrayListExtra("contacts"); if(ActivityCompat.checkSelfPermission(message.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() { @Override public void onComplete(@NonNull Task<Location> task) { Location location = task.getResult(); if (location != null){ try { Geocoder geocoder = new Geocoder(message.this, Locale.getDefault()); List<Address> address = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); Log.d("address", address.get(0).getAddressLine(0)); edittext.setText(address.get(0).getAddressLine(0)); sos_message(); } catch (IOException e) { e.printStackTrace(); } } } }); }else ActivityCompat.requestPermissions(message.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 44); } private void sos_message() { send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(ActivityCompat.checkSelfPermission(message.this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED){ try { SmsManager sms = SmsManager.getDefault(); if ((contacts != null) || (edittext.getText().toString().length() != 0)) { for (int i = 0; i < contacts.size(); i++) { sms.sendTextMessage(String.valueOf(contacts.get(i)), null, edittext.getText().toString(), null, null); } Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "Message sent successfully", Snackbar.LENGTH_LONG); snackbar.show(); } else { Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "Please try again", Snackbar.LENGTH_LONG); snackbar.show(); } } catch (Exception e) { e.printStackTrace(); } } else ActivityCompat.requestPermissions(message.this, new String[]{Manifest.permission.SEND_SMS}, 44); } }); } }
My AndroidManifest.xml permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.SEND_SMS" />
https://stackoverflow.com/questions/66486441/edittext-field-completely-empty-after-using-settext March 05, 2021 at 11:40AM
没有评论:
发表评论