【问题标题】:returning latitude or longitude array返回纬度或经度数组
【发布时间】:2013-12-28 14:45:44
【问题描述】:

我有返回位置纬度的方法

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);
 }

给我 nullpointor 异常,但在 logcat 中我可以看到显示的纬度数组 问题在于返回纬度。 纠正我哪里错了..

【问题讨论】:

    标签: android arrays latitude-longitude


    【解决方案1】:

    你不能在onCreate() 方法中使用setContentView()..

    tv.setText(sb); ,所以这一行给你错误..因为它找不到 textView。

    【讨论】:

      【解决方案2】:

      你的变量 sb 是一个 StringBuilder,请更新你的代码

      tv.setText(sb.toString());

      【讨论】:

        猜你喜欢
        • 2013-09-09
        • 2011-03-11
        • 1970-01-01
        • 2014-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多