Android Studio Java谷歌地图:为循环中断应用程序绘制多个标记
我是一个编程新手,我真的被这个问题困住了,虽然这个问题很小,但我已经尝试解决它好几天了,但都没有成功。 我从一个txt文件中读取标记的位置,将它们添加到ArrayList中,然后解析字符串以获取数据以添加新的标记。 当我一对一地做时,这不会有任何问题,但只要我想使用for循环,应用程序就会崩溃,并且不会绘制标记。 你能帮帮我吗?提前多谢Android Studio Java谷歌地图:为循环中断应用程序绘制多个标记,java,google-maps,android-studio,for-loop,google-maps-api-3,Java,Google Maps,Android Studio,For Loop,Google Maps Api 3,我是一个编程新手,我真的被这个问题困住了,虽然这个问题很小,但我已经尝试解决它好几天了,但都没有成功。 我从一个txt文件中读取标记的位置,将它们添加到ArrayList中,然后解析字符串以获取数据以添加新的标记。 当我一对一地做时,这不会有任何问题,但只要我想使用for循环,应用程序就会崩溃,并且不会绘制标记。 你能帮帮我吗?提前多谢 ArrayList<String> contents = new ArrayList<String>(); public void L
ArrayList<String> contents = new ArrayList<String>();
public void LoadLocations() throws IOException {
String str="";
StringBuffer buf = new StringBuffer();
InputStream is = this.getResources().openRawResource(R.raw.where_water1);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
if (is!=null) {
while ((str = reader.readLine()) != null) {
contents.add(str);
}
}
is.close();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//shows the map
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
configureLocationUpdates();
try {
LoadLocations();
} catch (IOException e) {
Toast.makeText(getApplicationContext(),
"Problems: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
但是,如果我将此代码放入for循环,应用程序将崩溃:
for (int i = 0; i<contents.size(); i++){
String[] splittedI = contents.get(i).split(",");
double latI = Double.parseDouble(splittedI[0]);
double lngI = Double.parseDouble(splittedI[1]);
LatLng fountainLocI = new LatLng(latI, lngI);
String addressI = splittedI[2] + ", " + splittedI[3];
m_map.addMarker(new MarkerOptions()
.position(fountainLocI)
.title(addressI)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));}
for(int i=0;iBetter使用for(String line:contents){
而不是for(int i=0;iHi,非常感谢您的回复。我发现了我的错误,字符串周围有一些空格,我想解析为双倍。Duh…非常感谢您的努力和提示!
for (int i = 0; i<contents.size(); i++){
String[] splittedI = contents.get(i).split(",");
double latI = Double.parseDouble(splittedI[0]);
double lngI = Double.parseDouble(splittedI[1]);
LatLng fountainLocI = new LatLng(latI, lngI);
String addressI = splittedI[2] + ", " + splittedI[3];
m_map.addMarker(new MarkerOptions()
.position(fountainLocI)
.title(addressI)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));}