2011-11-08 56 views
0

我有方法,該方法返回的位置返回緯度或經度陣列

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中,我可以看到顯示的緯度數組 問題在於回到緯度。 糾正我在哪裏,我錯了..

回答

1

你沒設的onCreate()方法使用setContentView() ..

tv.setText(sb);,所以此行是給你的錯誤..因爲它無法找到的TextView。

1

您的變量某人是一個StringBuilder,請更新您的代碼,這

tv.setText(sb.toString());