返回纬度或经度数组
我有返回位置纬度的方法
public double[] getlat(){
double lat[]=new double[20];
JSONObject json = JSONFunction.getJSONfromURL("https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=1000&types=bank&sensor=true&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
try{
JSONArray JArray = json.getJSONArray("results");
Log.v(TAG, "getting results");
for(int i=0;i<JArray.length();i++){
JSONObject e = JArray.getJSONObject(i);
JSONObject location=e.getJSONObject("geometry").getJSONObject("location");
lat[i] = location.getDouble("lat");
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
return lat;
}
我在主要活动中收到了这个数组,作为
public class Test extends Activity {
private static final String TAG = "test";
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle icicle) {
// Be sure to call the super class.
super.onCreate(icicle);
StringBuilder sb =new StringBuilder();
double a[]=getlat();
for(int i=0;i<a.length;i++) {
sb.append(a[i]+"\n");
}
TextView tv=(TextView)findViewById(R.id.text);
tv.setText(sb);
}
给我空指针异常,但在 logcat 中我可以看到显示的纬度数组 问题在于返回纬度。 纠正我哪里错了..
I have method which return the latitude of the location
public double[] getlat(){
double lat[]=new double[20];
JSONObject json = JSONFunction.getJSONfromURL("https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=1000&types=bank&sensor=true&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
try{
JSONArray JArray = json.getJSONArray("results");
Log.v(TAG, "getting results");
for(int i=0;i<JArray.length();i++){
JSONObject e = JArray.getJSONObject(i);
JSONObject location=e.getJSONObject("geometry").getJSONObject("location");
lat[i] = location.getDouble("lat");
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
return lat;
}
I recieved this array in main activity as
public class Test extends Activity {
private static final String TAG = "test";
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle icicle) {
// Be sure to call the super class.
super.onCreate(icicle);
StringBuilder sb =new StringBuilder();
double a[]=getlat();
for(int i=0;i<a.length;i++) {
sb.append(a[i]+"\n");
}
TextView tv=(TextView)findViewById(R.id.text);
tv.setText(sb);
}
Giving me nullpointor exception but in logcat i can see the latitude array displayed
The problem is in returing the lat.
Correct me where i am wrong..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有在
onCreate()
方法中使用setContentView()
..tv.setText(sb);
,所以这一行给出你的错误..因为它找不到textView。you din't use
setContentView()
in theonCreate()
method..tv.setText(sb);
, so this line is giving you the error.. as it can't find the textView.您的变量 sb 是一个 StringBuilder,请更新您的代码此
tv.setText(sb.toString());
Your Variable sb is a StringBuilder, kindly update your code this
tv.setText(sb.toString());