TypedArray是obtainTypedArray电话后空 [英] TypedArray is empty after obtainTypedArray call

查看:461
本文介绍了TypedArray是obtainTypedArray电话后空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的谷歌地图的实现,我充填到二类型数组从我的strings.xml双打,所以我可以用它们来填补的经度和纬度。

下面是方法:

  @覆盖
公共无效onMa pready(GoogleMap的地图){   map.setMyLocationEnabled(真);
   。TypedArray拉特= getResources()obtainTypedArray(R.array.Lats);
   TypedArray多头= getResources()obtainTypedArray(R.array.Longs)。
   INT长度= longs.getIndexCount();
    Log.e(我的应用程序日志,这里);
    Log.e(我的应用程序日志,长度=+长);
    的for(int i = 0; I<长度;我+ +)
    {
        的LatLng坐标=新经纬度(lats.getFloat(I,0),longs.getFloat(I,0));
        Log.e(我的应用程序日志,here2);
        Log.e(我的应用程序日志,纬度=+ lats.getFloat(I,0));        map.addMarker(新的MarkerOptions()
                .title伪(悉尼)
                是.snippet(人口最多的城市在澳大利亚。)
                .POSITION(坐标));
    }
    的LatLng坐标=新经纬度(lats.getFloat(0,0),longs.getFloat(0,0));    map.moveCamera(CameraUpdateFactory.newLatLngZoom(坐标,13));
}

我的strings.xml:

 <数组名=拉图>
    <项目> -33.867< /项目>
    <项目> -33.869< /项目>
    <项目> -33.871< /项目>
    <项目> -33.873< /项目>
    <项目> -33.876< /项目>
    <项目> -33.881< /项目>
    <项目> -33.883< /项目>
    <项目> -33.885< /项目>
    <项目> -33.891< /项目>
< /阵列><数组名=多头>
    <项目> 151.206< /项目>
    <项目> 151.209< /项目>
    <项目> 151.211< /项目>
    <项目> 151.205< /项目>
    <项目> 151.204< /项目>
    <项目> 151.207< /项目>
    <项目> 151.203< /项目>
    <项目> 151.201< /项目>
    <项目> 151.200< /项目>
< /阵列>

在logcat中显示:

  5月4日至30日:00:00.727 24986-24986 / com.example.andrew.ubair4 E /我的应用程序日志:这里
5月4日至三十〇日:00:00.727 24986-24986 / com.example.andrew.ubair4 E /我的应用程序日志:长度= 0


解决方案

尝试定义拉特和长为字符串数组中的字符串资源:

 <字符串数组名=拉特>
    <项目> -33.867< /项目>
    <项目> -33.869< /项目>
    <项目> -33.871< /项目>
    <项目> -33.873< /项目>
    <项目> -33.876< /项目>
    <项目> -33.881< /项目>
    <项目> -33.883< /项目>
    <项目> -33.885< /项目>
    <项目> -33.891< /项目>
< /字符串数组><字符串数组名=多头>
    <项目> 151.206< /项目>
    <项目> 151.209< /项目>
    <项目> 151.211< /项目>
    <项目> 151.205< /项目>
    <项目> 151.204< /项目>
    <项目> 151.207< /项目>
    <项目> 151.203< /项目>
    <项目> 151.201< /项目>
    <项目> 151.200< /项目>
< /字符串数组>。的String [] =拉特getResources()getStringArray(R.array.lats);
的String [] =多头getResources()getStringArray(R.array.longs)。的for(int i = 0; I< longs.length;我++){
     坐标的LatLng =新经纬度(Double.parseDouble(拉特[I]),Double.parseDouble(多头[I]));
     map.addMarker(新的MarkerOptions()
        .title伪(悉尼)
        是.snippet(人口最多的城市在澳大利亚。)
        .POSITION(坐标));
}

I have a simple google maps implementation, I'm filling two typed arrays with doubles from my strings.xml so I can use them to fill in as longitudes and latitudes.

Here is the method:

@Override
public void onMapReady(GoogleMap map) {

   map.setMyLocationEnabled(true);
   TypedArray lats = getResources().obtainTypedArray(R.array.Lats);
   TypedArray longs = getResources().obtainTypedArray(R.array.Longs);
   int length = longs.getIndexCount();
    Log.e("MY APP LOG", "here");
    Log.e("MY APP LOG", "length = " + length);
    for(int i = 0; i < length; i++)
    {
        LatLng coord = new LatLng(lats.getFloat(i,0),longs.getFloat(i,0));
        Log.e("MY APP LOG", "here2");
        Log.e("MY APP LOG", "latitude = " + lats.getFloat(i,0));

        map.addMarker(new MarkerOptions()
                .title("Sydney")
                .snippet("The most populous city in Australia.")
                .position(coord));
    }
    LatLng coord = new LatLng(lats.getFloat(0,0),longs.getFloat(0,0));

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(coord, 13));


}

My strings.xml:

<array name="Lats">
    <item>-33.867</item>
    <item>-33.869</item>
    <item>-33.871</item>
    <item>-33.873</item>
    <item>-33.876</item>
    <item>-33.881</item>
    <item>-33.883</item>
    <item>-33.885</item>
    <item>-33.891</item>
</array>

<array name="Longs">
    <item>151.206</item>
    <item>151.209</item>
    <item>151.211</item>
    <item>151.205</item>
    <item>151.204</item>
    <item>151.207</item>
    <item>151.203</item>
    <item>151.201</item>
    <item>151.200</item>
</array>

The logcat shows:

04-30 05:00:00.727  24986-24986/com.example.andrew.ubair4 E/MY APP LOG﹕ here
04-30 05:00:00.727  24986-24986/com.example.andrew.ubair4 E/MY APP LOG﹕ length = 0

解决方案

Try to define lats and longs as String-Array in String resource :

<string-array name="lats">
    <item>-33.867</item>
    <item>-33.869</item>
    <item>-33.871</item>
    <item>-33.873</item>
    <item>-33.876</item>
    <item>-33.881</item>
    <item>-33.883</item>
    <item>-33.885</item>
    <item>-33.891</item>
</string-array>

<string-array name="longs">
    <item>151.206</item>
    <item>151.209</item>
    <item>151.211</item>
    <item>151.205</item>
    <item>151.204</item>
    <item>151.207</item>
    <item>151.203</item>
    <item>151.201</item>
    <item>151.200</item>
</string-array>

String[] lats = getResources().getStringArray(R.array.lats);
String[] longs = getResources().getStringArray(R.array.longs);

for(int i=0;i<longs.length;i++){
     LatLng coord = new LatLng(Double.parseDouble(lats[i]),Double.parseDouble(longs[i]));
     map.addMarker(new MarkerOptions()
        .title("Sydney")
        .snippet("The most populous city in Australia.")
        .position(coord));
}

这篇关于TypedArray是obtainTypedArray电话后空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆