[英]Edittext field completely empty, after using settext()
I am trying to insert the address fetched from the geocoder
API, into my edittext
field.我正在尝试将从
geocoder
器edittext
获取的地址插入到我的编辑文本字段中。 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.在我的应用程序中授予位置权限后,我的编辑
edittext
字段中没有插入任何文本,甚至也没有调用 SMS 权限。
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:我的AndroidManifest.xml权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
Thanks to @Kidus Tekeste, I discovered that my location
object was actually showing to be null.感谢@Kidus Tekeste,我发现我的
location
object 实际上显示为 null。 This piece of code solves the problem:这段代码解决了这个问题:
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10 * 1000); // 10 seconds
locationRequest.setFastestInterval(5 * 1000); // 5 seconds
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
return;
}
for (Location location : locationResult.getLocations()) {
if (location != null) {
stringBuilder.append(location.getLatitude());
stringBuilder.append("-");
stringBuilder.append(location.getLongitude());
stringBuilder.append("\n\n");
Toast.makeText(message.this,stringBuilder,Toast.LENGTH_SHORT).show();
//txtContinueLocation.setText(stringBuilder.toString());
if (fusedLocationProviderClient != null) {
fusedLocationProviderClient.removeLocationUpdates(locationCallback);
}
}
}
}
};
if((ActivityCompat.checkSelfPermission(message.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
&& (ActivityCompat.checkSelfPermission(message.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) ){
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null){
Geocoder geocoder = new Geocoder(message.this, Locale.getDefault());
List<Address> address = null;
try {
address = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
} catch (IOException e) {
e.printStackTrace();
}
Log.d("address", address.get(0).getAddressLine(0));
edittext.setText(address.get(0).getAddressLine(0));
sos_message();
}
else{
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "Try again", Snackbar.LENGTH_LONG);
snackbar.show();
}
}
});
}else
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION},
44);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.