其他分享
首页 > 其他分享> > android – 谷歌地图在少数设备中崩溃

android – 谷歌地图在少数设备中崩溃

作者:互联网

我的应用程序在Moto G(Android 5.0.2)中运行良好,但在Karbon spark(Android 5.1.1)和其他几个设备中崩溃.这次崩溃是由于MapFragment造成的. Min skd是17,所以我使用的是MapFragment.

AndroidManifest.xml中

<permission android:name="com.dashboard.Activity.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.dashboard.Activity.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<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="@string/api_key" />

的build.gradle

编译’com.google.android.gms:play-services-maps:8.4.0′

activity_main.xml中

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

Activity.java

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); //App crashes here
}

————错误日志———

Unable to start activity ComponentInfo{ com.dashboard.Activity.ActivityA}:
android.view.InflateException: Binary XML file line #10: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at android.app.Activity.setContentView(Activity.java:2145)
at com.dashboard.Activity.ActivityA.onCreate(ActivityA.java:62)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)

解决方法:

我今天遇到了同样的问题,这让我疯了.

如果你正在运行findbyID(R.id.map);评论出来.碎片不应该以这种方式膨胀.

尝试将类从MapFragment转换为SupportMapFragment

此外,处理地图片段的类应该扩展SupportMapFragment,这是我的样子:

公共类MapFragment扩展SupportMapFragment实现OnMapReadyCallback {

我的MapFramgment.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment
    android:name="com.mypackage.android.Views.MapFragment"
    android:id="@+id/map_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    class="com.google.android.gms.maps.SupportMapFragment"
    xmlns:android="http://schemas.android.com/apk/res/android">
    </fragment>

祝好运!

标签:android,google-maps,supportmapfragment,mapfragment
来源: https://codeday.me/bug/20190702/1356322.html