我想让程序能够使用Location location.getLatitude()/ Location location.getLongitude方法返回上面a和b值的值,但每当我用一个返回double的方法替换a和b时getLongitude和getLatitude提供的值或实际的get函数本身,我收到错误。
public void onClick(View v) {
requesturl = "https://maps.googleapis.com/maps/api/place/search/json?" +
"location=" + a + "," + b + "&radius=6000&" + "types=bank&sensor=false&key=AIzaSyDXNlHRDZnWW0T0tvBUjpyA8k2K9sjS2cM";
HHand(requesturl);
}
我怎样才能让a和b从getLatitude()和getLongitude()函数中获取它们的值?
已编辑:代码
public class Freedom extends ActivityGroup implements LocationListener {
/** Called when the activity is first created.
* @return */
String keystring="";
String requesturl;
LocationManager locman;
Location location;
//Awesome
public String convertStreamToString(InputStream is) {
return new Scanner(is).useDelimiter("\\A").next();
}
public void HHand(String a) {
//HTTP Request Processing for URL
HttpClient client=new DefaultHttpClient();
StringBuilder builder=new StringBuilder(a);
builder.append(URLEncoder.encode(keystring));
HttpPost post=new HttpPost(a);
try {
org.apache.http.HttpResponse response=client.execute(post);
HttpEntity entity=response.getEntity();
if (entity != null) {
InputStream is = entity.getContent();
String val = convertStreamToString(is);
Log.e("", val);
}
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locman = (LocationManager) this.getSystemService(LOCATION_SERVICE);
}
protected void onResume() {
super.onResume();
locman.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this);
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.btmmnu, menu);
return true;
}
protected Dialog onCreateDialog(int id) {
final Dialog dialog = new Dialog(this);
OnClickListener txtlstn = new OnClickListener() {
public void onClick(View v) {
dismissDialog(0);
}
};
switch(id) {
case 0:
...
}
return dialog;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.About:
showDialog(0);
return true;
case R.id.Guidance:
final Button Travel = (Button) findViewById(R.id.travel);
Travel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
requesturl = "https://maps.googleapis.com/maps/api/place/search/json?" +
"location=" + location.getLatitude() + "," + location.getLongitude() + "&radius=6000&" +
"types=car_rental&sensor=false&key=AIzaSyDXNlHRDZnWW0T0tvBUjpyA8k2K9sjS2cM";
HHand(requesturl);
}
});
答案 0 :(得分:0)
如果您从NullPointerException
或location.getLatitude()
获得location.getLongitude()
,则表示location
为空。
您尚未显示location
来自哪里,或者您看到方法调用返回a
和b
的位置,但这将是原因,基本上。也许你有一个隐藏实例变量的局部变量?例如:
public class Foo
{
private Location location;
public Foo(double latitude, double longitude)
{
// Careful - this declares a new *local* variable, so it's not
// changing the value of this.location
Location location = new Location();
location.setLatitude(latitude);
location.setLongitude(longitude);
...
}
public void bang()
{
// This will throw, as location is still null
double x = location.getLatitude();
}
}