使用 settext() 后,Edittext 字段完全为空
Edittext field completely empty, after using settext()
我正在尝试将从 geocoder
API 获取的地址插入我的 edittext
字段。在我的应用程序中授予位置权限后,我的 edittext
字段中没有插入任何文本,甚至没有调用 SMS 权限。
我的代码:
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);
}
});
}
}
我的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" />
感谢@Kidus Tekeste,我发现我的 location
对象实际上显示为空。这段代码解决问题:
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);
我正在尝试将从 geocoder
API 获取的地址插入我的 edittext
字段。在我的应用程序中授予位置权限后,我的 edittext
字段中没有插入任何文本,甚至没有调用 SMS 权限。
我的代码:
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);
}
});
}
}
我的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" />
感谢@Kidus Tekeste,我发现我的 location
对象实际上显示为空。这段代码解决问题:
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);