mapFragment.getMapAsync中的NullPointerException

时间:2016-04-25 04:30:46

标签: java android google-maps android-manifest supportmapfragment

在经历了与我的头衔相关的SO问题之后,我无法找到问题的解决方案。我在NullPointerExceptionmapFragment.getMapAsync。以下是我的MapActivity代码。

package com.example.jerofad.mylocation;
import android.app.FragmentManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements  OnMapReadyCallback {
    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) this.getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        mMap.getUiSettings().setZoomControlsEnabled(true);
    }
}

我正在使用google play service 8.4.0。我的Android Manifest如下所示。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jerofad.mylocation">

<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but you must specify either coarse or fine
     location permissions for the 'MyLocation' functionality. 
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="22"/>
<permission
    android:name="com.example.jerofad.mylocation.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.example.jerofad.mylocation.permission.MAPS_RECEIVE" />
<uses-permission
    android:name="android.permission.READ_EXTERNAL_STORAGE"
    android:maxSdkVersion="22" />
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/mylocation"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">        
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps">

    </activity>
    <activity
        android:name=".LauncherActivity"
        android:label="@string/title_activity_launcher"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

如何在mapFragment.getMapAsync(this)修复错误。提前致谢。这是崩溃日志。

04-25 11:11:49.079 31448-31448/com.example.jerofad.mylocation 
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jerofad.mylocation, PID: 31448
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jerofad.mylocation/com.example.jerofad.mylocation.MapsActivity}: java.lang.NullPointerException
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
   at android.app.ActivityThread.access$800(ActivityThread.java:151)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
   at android.os.Handler.dispatchMessage(Handler.java:110)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:5299)
   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:825)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
   at com.example.jerofad.mylocation.MapsActivity.onCreate(MapsActivity.java:24)
   at android.app.Activity.performCreate(Activity.java:5423)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
   at android.app.ActivityThread.access$800(ActivityThread.java:151) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
   at android.os.Handler.dispatchMessage(Handler.java:110) 
   at android.os.Looper.loop(Looper.java:193) 
   at android.app.ActivityThread.main(ActivityThread.java:5299) 
   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:825) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641) 
   at dalvik.system.NativeStart.main(Native Method)

这是activity_maps.xml。

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    class ="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.jerofad.mylocation.MapsActivity" />

4 个答案:

答案 0 :(得分:6)

请试试这个,

删除/评论SupportMapfragment行一段时间 试试这个,

MapFragment mapFragment = (MapFragment) getFragmentManager() .findFragmentById(R.id.map);

答案 1 :(得分:4)

你有两个选择,一个是Raghavendra告诉另一个是你可以改变类属性

class ="com.google.android.gms.maps.MapFragment"

class="com.google.android.gms.maps.SupportMapFragment" 

然后你的代码就可以了。

答案 2 :(得分:1)

你可以试试这个......这可能有帮助...

    @Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.i("onActivityCreated", "onActivityCreated");
    if(supportMapFragment==null){
        supportMapFragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);

        supportMapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mGoogleMap = googleMap;

                loadMapAtPosition(googleMap, CAMERA_INITIAL_LATITUDE, CAMERA_INITIAL_LONGITUDE);
            }
        });
}

答案 3 :(得分:1)

如果这对任何人都有帮助..

添加&lt;使用的许可&GT; NETWORK_ACCESS_STATE到您的清单。