Android的SupportMapFragment异常错误(Android SupportMapF

2019-07-04 17:18发布

我试图使用新的谷歌游戏服务库和embbed在侧地图的地图片段如下

PlaceMapsFragment.java

public class PlaceMapsFragment extends SupportMapFragment {
    private GoogleMap mMap;
    private LatLng mPosFija;

    public PlaceMapsFragment() {
        super();
    }

    public static PlaceMapsFragment newInstance(LatLng posicion) {
        PlaceMapsFragment frag = new PlaceMapsFragment();
        frag.mPosFija = posicion;
        return frag;
    }

    @Override
    public GoogleMap getMap() {
        // TODO Auto-generated method stub
        return super.getMap();
    }

    @Override
    public void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        //initMap();
        return view;
    }

    private void initMap() {
        UiSettings settings = getMap().getUiSettings();
        settings.setAllGesturesEnabled(false);
        settings.setMyLocationButtonEnabled(false);

        getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(mPosFija, 16));
        getMap().addMarker(
                new MarkerOptions().position(mPosFija)
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.marker)));
    }

}

错误

12-10 01:44:54.416: E/AndroidRuntime(32716): FATAL EXCEPTION: main
12-10 01:44:54.416: E/AndroidRuntime(32716): java.lang.NullPointerException
12-10 01:44:54.416: E/AndroidRuntime(32716):    at maps.ar.b.a(Unknown Source)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at maps.y.h.a(Unknown Source)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at maps.y.au.a(Unknown Source)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at maps.y.ae.moveCamera(Unknown Source)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at com.google.android.gms.maps.internal.IGoogleMapDelegate$Stub.onTransact(IGoogleMapDelegate.java:83)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.os.Binder.transact(Binder.java:297)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.moveCamera(Unknown Source)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at com.m7.nomad.PlaceMapsFragment.initMap(PlaceMapsFragment.java:55)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at com.m7.nomad.PlaceMapsFragment.onCreateView(PlaceMapsFragment.java:46)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at com.m7.nomad.PlaceActivity$TabManager.onTabChanged(PlaceActivity.java:153)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:379)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.widget.TabHost.setCurrentTab(TabHost.java:364)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:540)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.view.View.performClick(View.java:3591)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.view.View$PerformClick.run(View.java:14263)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.os.Handler.handleCallback(Handler.java:605)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.os.Looper.loop(Looper.java:137)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.app.ActivityThread.main(ActivityThread.java:4507)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at java.lang.reflect.Method.invokeNative(Native Method)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at java.lang.reflect.Method.invoke(Method.java:511)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at dalvik.system.NativeStart.main(Native Method)

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.m7.nomad"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <permission
        android:name="com.example.newmapview.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.newmapview.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock" >
        <activity
            android:name="com.m7.nomad.SplashActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.m7.nomad.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.m7.nomad.PlaceActivity"
            android:label="@string/app_name" >
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="XXXXXXXXXXX" />
    </application>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

</manifest>

我有一个具有附加了两个片段一个TabHost一个SherlockFragmentActivity。 我想呈现地图内的标签片段之一

编辑

我有要显示的地图,但现在我不能够参照地图和更改地图混合。 基本上,我需要出去获得零误差参考地图。

Answer 1:

onCreateView()必须调用它的父。 检查在溶液中https://stackoverflow.com/a/13734470/835787接近。 该地图是在MapFragment的onCreateView()方法进行初始化,如果你不把它在地图将是空的。



Answer 2:

  1. super.onCreate(savedInstanceState); 应的onCreate()被调用,而不是onCreateView()
  2. 如果你想覆盖onCreateView(),它应该是这样的:

     @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = super.onCreateView(inflater, container, savedInstanceState); // Do any other initialization you need, but note that the MapView already exists inside v setUpMapIfNeeded(v); return v; } 


Answer 3:

我不知道的是它可以帮助或没有,但你应该在这里设置你的包:

    <permission
    android:name="com.example.newmapview.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.newmapview.permission.MAPS_RECEIVE" />


Answer 4:

我通过检查“ 复制项目进入工作区 ”,同时导入谷歌播放服务的lib解决了这个错误。
更多资讯: http://developer.android.com/google/play-services/setup.html



文章来源: Android SupportMapFragment Exception Error