当我尝试使用地图时获取java.lang.NullPointerException

时间:2015-04-14 23:01:23

标签: java android google-maps

首先,我正在使用Android Studio,这是我第一次使用地图。

我创建了一个带有导航抽屉活动的项目。

文件>新项目>下一个>下一个>切换“导航抽屉活动”并单击下一步>完成。

之后,我用右键>点击了项目; 新>活动>谷歌>谷歌地图活动。

我将google api maps键放在google_maps_api.xml中。

所以我打开了MainActivity,并在OnCreateView函数中输入以下内容:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_maps, container, false);
    setUpMapIfNeeded();
    return rootView;
}

然后我添加以下两个函数:

 private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
   }    
private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }

每当我尝试执行它时,我都会得到一个空指针异常。以下是完整的错误消息:

04-14 19:44:40.204  4390-4390/ct.ufrn.br.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: ct.ufrn.br.myapplication, PID: 4390
    java.lang.RuntimeException: Unable to start activity ComponentInfo{ct.ufrn.br.myapplication/ct.ufrn.br.myapplication.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
            at ct.ufrn.br.myapplication.MainActivity$PlaceholderFragment.onCreateView(MainActivity.java:142)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1194)
            at android.app.Activity.performStart(Activity.java:5258)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2171)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

你知道怎么解决吗?

obs:我尝试使用空白活动(在onCreate方法中,而不是onCreateView),并且效果很好。

这是activity_maps.xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/map"
tools:context="ct.ufrn.br.myapplication.MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />

1 个答案:

答案 0 :(得分:0)

我解决了替换

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

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