【问题标题】:StrictMode error with Google Maps v2 @ Android fragmentGoogle Maps v2 @ Android 片段的 StrictMode 错误
【发布时间】:2014-11-12 03:51:08
【问题描述】:

在我的一个片段中,我想显示一项 Google 地图服务。但我得到一个 StrictMode 错误:

 1899-1903/com.android.calendar E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called

我已经在 SDK 中下载了 Google Play Services、Repository 和 Google API。我已经在我的 Android Studio gradle build 中设置了依赖项:

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 compile 'com.android.support:support-v4:21.0.0'
 compile 'com.android.support:appcompat-v7:21.0.+'
 compile 'com.google.android.gms:play-services:6.1.+'
}

我的清单设置如下所示:

...
<permission
    android:name="org.example.mapapp.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="org.example.mapapp.permission.MAPS_RECIEVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />
...
<application>
...
<meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="myApiKey" />
...

在我的 Fragment 中,我是这样设置的:

public class MapFragment extends Fragment {
    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    static final LatLng KIEL = new LatLng(53.551, 9.993);
    private static GoogleMap map;

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_layout, container, false);

    map = ((SupportMapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
    if (map!=null){
        Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
                .title("Hamburg"));
        Marker kiel = map.addMarker(new MarkerOptions()
                .position(KIEL)
                .title("Kiel")
                .snippet("Kiel is cool")
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.ic_launcher)));
    }

    return rootView;
    }
}

我的 fragment_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

 <fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

【问题讨论】:

  • MapFragment.java 中的49 行是什么?
  • map = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();

标签: java android google-play-services android-maps-v2


【解决方案1】:

在你的行中:

map = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();

您正在将 getMap() 类型转换为 MapFragment,但在您的片段类中:

<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />

您正在使用“com.google.android.gms.maps.SupportMapFragment”类。所以你必须将类型转换从 MapFragment 更改为 SupportMapFragment

【讨论】:

  • 谢尔兹你是对的。这实际上是因为如果复制粘贴太多 - 我已将其更正为 SupportMapFragment。现在我收到此错误:1899-1903/com.android.calendar E/StrictMode:在附加的堆栈跟踪中获取了资源,但从未释放。有关避免资源泄漏的信息,请参阅 java.io.Closeable。 java.lang.Throwable:未调用显式终止方法“关闭”
  • 看来是谷歌地图的bug。这是一个有问题的链接:code.google.com/p/gmaps-api-issues/issues/…
  • 啊哈,现在看起来我能做的不多。。谢谢你的链接!
猜你喜欢
  • 2014-06-06
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
  • 1970-01-01
相关资源
最近更新 更多