Android setUpMapIfNeeded();给出空指针异常

时间:2015-01-16 18:15:26

标签: android

所以我刚创建了一个新的谷歌地图活动,如果我尝试运行我的应用程序,它会给我一个空指针异常

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

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

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

 private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}

 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) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

这是我的布局

<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="android.ehb.be.lab4sqlliteex.MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>

当我添加我的活动时,它只是由android studio自动生成的。 我的课程扩展了FragmentActivity

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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/google_maps_key"/>


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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyBxMzLA6jjVtPQEHOrOPTRzKnjPu6bKKGA"
    />
    <activity
        android:name="android.ehb.be.lab4sqlliteex.MapsActivity"
        android:label="@string/title_activity_maps">
    </activity>
    <activity
        android:name=".BucketListActivity"
        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=".AlbumsActivity"
        android:label="Albums"
        android:parentActivityName=".BucketListActivity">

    </activity>
</application>

有人知道如何解决这个问题吗?

这是我得到的错误:

  java.lang.RuntimeException: Unable to start activity ComponentInfo{android.ehb.be.lab4sqlliteex/android.ehb.be.lab4sqlliteex.MapsActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
        at android.app.ActivityThread.access$800(ActivityThread.java:156)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5867)
        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:858)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.ehb.be.lab4sqlliteex.MapsActivity.setUpMapIfNeeded(MapsActivity.java:46)
        at android.ehb.be.lab4sqlliteex.MapsActivity.onCreate(MapsActivity.java:18)

2 个答案:

答案 0 :(得分:1)

你需要调用onCreate():setContentView(R.layout.main_layout)。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    setUpMapIfNeeded();
}

答案 1 :(得分:1)

请确保您扩展 ActionBarActivity

public class Contact_Us扩展ActionBarActivity {     私人GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contact_us);
    setUpMapIfNeeded();
}