本文介绍了Android的GoogleMap的或SupportMapFragment - 空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我表示片段谷歌地图版本2。但我得到空指针异常在

MMAP =((SupportMapFragment)getFragmentManager()findFragmentById(R.id.map)。)的GetMap();

这是我的全部code:

 公共类SearchResultMap扩展片段{

    私人GoogleMap的MMAP;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        查看fragmentView = inflater.inflate(R.layout.map,集装箱,假);

        MMAP =((SupportMapFragment)getFragmentManager()findFragmentById(R.id.map)。)的GetMap()。 //线路班次:28

         mMap.addMarker(新MarkerOptions().position(新经纬度(XXXXXX,XXXXXX)).title伪(当前位置)
         .icon(BitmapDesc​​riptorFactory.fromResource(R.drawable.ball_pointer))
         是.snippet(XXXXX));

        返回fragmentView;
    }

    @覆盖
    公共无效onActivityCreated(包savedInstanceState){
        super.onActivityCreated(savedInstanceState);
    }

}
 

XML:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>

   <片段
        机器人:ID =@ + ID /图
        机器人:名称=com.google.android.gms.maps.SupportMapFragment
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>

< / LinearLayout中>
 

清单XML:

 <舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.fssd.spot
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <用途-SDK
        安卓的minSdkVersion =8
        机器人:targetSdkVersion =17/>

    <许可
        机器人:名称=com.fssd.spot.permission.MAPS_RECEIVE
        安卓的ProtectionLevel =签名/>

    <用途特征
        机器人:glEsVersion =0x00020000
        机器人:要求=真/>

    <使用-权限的Andr​​oid:名称=com.fssd.spot.permission.MAPS_RECEIVE/>
    <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
    <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
    <使用-权限的Andr​​oid:名称=com.google.android.providers.gsf.permission.READ_GSERVICES/>
    <使用-权限的Andr​​oid:名称=android.permission.ACCESS_COARSE_LOCATION/>
    <使用-权限的Andr​​oid:名称=android.permission.ACCESS_FINE_LOCATION/>
    <使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>

    <应用
        机器人:allowBackup =真
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名称=com.fssd.spot.SplashScreen
            机器人:主题=@安卓风格/ Theme.Light.NoTitleBar
            机器人:noHistory =真
            机器人:screenOrientation =画像>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>

                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:名称=com.fssd.spot.MainActivity
            机器人:screenOrientation =画像
            机器人:主题=@风格/ tabTheme>
        < /活性GT;

        &所述;元数据
            机器人:名称=com.google.android.maps.v2.API_KEY
            机器人:值=xxxxxxxxxxxxxxxxxxxxxxxx/>

    < /用途>

< /舱单>
 

全日志的猫:

 致命异常:主要
显示java.lang.NullPointerException
在com.fssd.spot.search.SearchResultMap.onCreateView(SearchResultMap.java:28)
在android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
在android.support.v4.app.Fragment.performStart(Fragment.java:1499)
在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:957)
在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
在android.support.v4.app.FragmentManagerImpl $ 1.运行(FragmentManager.java:440)
在android.os.Handler.handleCallback(Handler.java:587)
在android.os.Handler.dispatchMessage(Handler.java:92)
在android.os.Looper.loop(Looper.java:130)
在android.app.ActivityThread.main(ActivityThread.java:3683)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:507)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:875)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
在dalvik.system.NativeStart.main(本机方法)
 

解决方案

试图改变这样的:

  FragmentManager myFM = getActivity()getSupportFragmentManager();

最后SupportMapFragment myMAPF =(SupportMapFragment)myFM
                .findFragmentById(R.id.map);
 

In my app i showing google map version2 in a fragment. but i get Null pointer exceptionat

mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

This is my full code:

    public class SearchResultMap extends Fragment{

    private GoogleMap mMap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View fragmentView = inflater.inflate(R.layout.map, container, false);

        mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); // line no : 28

         mMap.addMarker(new MarkerOptions() .position(new LatLng(xxxxxx,xxxxxx)) .title("Current Location")
         .icon(BitmapDescriptorFactory.fromResource(R.drawable.ball_pointer))
         .snippet("xxxxx"));

        return fragmentView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

}

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>

Manifest xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fssd.spot"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <permission
        android:name="com.fssd.spot.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

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

    <uses-permission android:name="com.fssd.spot.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <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-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.fssd.spot.SplashScreen"
            android:theme="@android:style/Theme.Light.NoTitleBar"
            android:noHistory="true"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.fssd.spot.MainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/tabTheme" >
        </activity>

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

    </application>

</manifest>

whole log cat:

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.fssd.spot.search.SearchResultMap.onCreateView(SearchResultMap.java:28)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.Fragment.performStart(Fragment.java:1499)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:957)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(Native Method)
解决方案

try to change this:

FragmentManager myFM = getActivity().getSupportFragmentManager();

final SupportMapFragment myMAPF = (SupportMapFragment) myFM
                .findFragmentById(R.id.map);

这篇关于Android的GoogleMap的或SupportMapFragment - 空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 20:03