【发布时间】:2012-01-04 01:28:14
【问题描述】:
在过去的两天里,我一直在尝试打印我的 lang 和 long 坐标,我已经砍掉并更改了代码以使其工作,现在有点混乱,不明白为什么它不会工作,我是 Android 开发的新手,同时查阅了一本书(开始 Android 开发),但它没有涉及这个主题,任何帮助将不胜感激。
它可以编译并且我没有收到任何错误,但是它不会打印到 main.xml 中定义的应用程序中的 EditText 框。
这是我的 Java 文件代码:
package com.emergency;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Context;
import android.location.LocationManager;
import android.location.Criteria;
public class EmergencyLocation extends Activity implements LocationListener {
private TextView latitudeField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
/** Called when the activity is first created. **/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
latitudeField = (TextView) findViewById(R.id.long_lat1);
longitudeField = (TextView) findViewById(R.id.long_lat2);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(provider, 0, 0, this);
Location location = locationManager.getLastKnownLocation(provider);
onLocationChanged(location);
}
@Override
protected void onDestroy() {
super.onDestroy();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latitudeField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
/*Toast.makeText(EmergencyLocation.this,
"Longitude " + longitudeField + "Latitude " + latitudeField, Toast.LENGTH_LONG).show();*/
//getting longitude to display in an EditText box
/*EditText lngtude = (EditText) findViewById(R.id.longitudeField);
lngtude.setText(String.valueOf(lng));
EditText lattude = (EditText) findViewById(R.id.latitudeField);
lattude.setText(String.valueOf(lat)); */
EditText lngtude = (EditText) findViewById(R.id.longitudeField);
lngtude.setText(String.valueOf(lng), TextView.BufferType.EDITABLE);
EditText lattude = (EditText) findViewById(R.id.latitudeField);
lattude.setText(String.valueOf(lat), TextView.BufferType.EDITABLE);
} else {
latitudeField.setText("Provider not available");
longitudeField.setText("Provider not available");
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
/* @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button simpleBtn = (Button) findViewById(R.id.location);
simpleBtn.setOnClickListener(new View.OnClickListener() {*/
}
;
【问题讨论】:
-
什么意思,没用?你没有得到预期值(或)你得到任何错误?
-
它不会打印到 main.xml 中定义的 EditText 框。没有错误,它编译。
-
你有没有调试过???使用 Log.d(...) 写入日志,而不是 System.out.println。
-
调试我被告知当您在 Eclipse 中按 F11 时,它会在启动模拟器之前完成。字面意思是“Log.d(”prov etc)”而不是“System.out.println”?
标签: android latitude-longitude android-edittext toast