【发布时间】:2017-02-04 15:20:26
【问题描述】:
我正在制作一个地理编码器反向地理编码器。
坐标在我想进一步用于反向地理编码的文本视图中生成。
问题是当我尝试将 textview 的内容保存到变量中时,应用程序突然停止工作。LATITUDE = Double.parseDouble(t.getText().toString());LATITUDE 是变量,t 是其中坐标的textView已生成。
有没有其他方法可以将 double 值从 textView 保存到变量中,以便进一步使用。
如果不行,能否解决这个问题。
在此先感谢:)
这是我的java代码:
public class MainActivity extends Activity {
private Button b;
public TextView t;
public TextView u;
public TextView add;
private LocationManager locationManager;
private LocationListener listener;
public Button b2;
private EditText e;
double LATITUDE;
double LONGITUDE;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (TextView) findViewById(R.id.textView);
u = (TextView) findViewById(R.id.textView3);
add = (TextView) findViewById(R.id.textView4);
b = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button2);
e = (EditText) findViewById(R.id.editText);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String number = e.getText().toString();
String loc = t.getText().toString();
String loc1 = u.getText().toString();
String loc2 = add.getText().toString();
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"+number));
sendIntent.putExtra("sms_body",loc+loc1+loc2);
try {
startActivity(sendIntent);
finish();
Log.i("Finished sending SMS...", "");
}
catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this,
"SMS failed, please try again later.", Toast.LENGTH_SHORT).show();
}
}
});
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
t.append("\n " + location.getLatitude());
u.append("\n " + location.getLongitude());
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
};
configure_button();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 10:
configure_button();
break;
default:
break;
}
}
void configure_button(){
// first check for permissions
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.INTERNET}
,10);
}
return;
}
// this code won't execute IF permissions are not allowed, because in the line above there is return statement.
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//noinspection MissingPermission
locationManager.requestLocationUpdates("gps",15000,0, listener);
}
});
}
public void phone(View view) {
Intent i = new Intent(this,Main2Activity.class);
startActivity(i);
}
public void address(View view) {
LATITUDE = Double.parseDouble(t.getText().toString());
LONGITUDE = Double.parseDouble(u.getText().toString());
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
String result;
try {
List<Address> addressList = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if (addressList != null && addressList.size() > 0) {
Address address = addressList.get(0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i));
}
sb.append("\n" + address.getLocality());
sb.append("\n" + address.getPostalCode());
sb.append("\n" + address.getCountryName());
result = sb.toString();
add.append(result);
}
}
catch (IOException e) {
}
}
}
【问题讨论】:
标签: java android textview double reverse-geocoding