Google Maps API v2抛出java.lang.runtimeexception:无法启动活动java.lang.nullpointerexception

时间:2014-03-16 12:02:21

标签: java android google-maps map

我一直试图让一个简单的应用程序运行,显示一个地图,但它不会工作。这是我使用的代码:

MainActivity.java:

package info.androidhive.googlemapsv2;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends FragmentActivity  {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get a handle to the Map Fragment
        GoogleMap map = ((MapFragment) getFragmentManager()
            .findFragmentById(R.id.map)).getMap();

        LatLng sydney = new LatLng(-33.867, 151.206);

        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

        map.addMarker(new MarkerOptions()
            .title("Sydney")
            .snippet("The most populous city in Australia.")
            .position(sydney));
    }
}

GoogleMapsV2 Manifest:

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

<permission
    android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />

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

<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" />

<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">

    <uses-library
        android:name="com.google.android.maps"
        android:required="true" />

    <activity
        android:name="info.androidhive.googlemapsv2.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppBaseTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <!-- Goolge API Key -->
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyCg5_f6_CdvyUBDuBf_gU8L39YyiQZdEh0" />

</application>

</manifest>

activity_main.xml中:

<RelativeLayout 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"
    tools:context=".MainActivity" >

<TextView
    android:id="@+id/tv_location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

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

</RelativeLayout>

logcat的:

03-16 12:53:10.778: W/dalvikvm(32321): threadid=1: thread exiting with uncaught exception (group=0x40db3600)
03-16 12:53:10.778: E/AndroidRuntime(32321): FATAL EXCEPTION: main
03-16 12:53:10.778: E/AndroidRuntime(32321): java.lang.RuntimeException: Unable to start     activity     ComponentInfo{info.androidhive.googlemapsv2/info.androidhive.googlemapsv2.MainActivity}:     java.lang.NullPointerException
03-16 12:53:10.778: E/AndroidRuntime(32321):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
03-16 12:53:10.778: E/AndroidRuntime(32321):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
03-16 12:53:10.778: E/AndroidRuntime(32321):    at android.app.ActivityThread.access$600(ActivityThread.java:151)
03-16 12:53:10.778: E/AndroidRuntime(32321):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
03-16 12:53:10.778: E/AndroidRuntime(32321):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 12:53:10.778: E/AndroidRuntime(32321):    at android.os.Looper.loop(Looper.java:155)
03-16 12:53:10.778: E/AndroidRuntime(32321):    at android.app.ActivityThread.main(ActivityThread.java:5536)
03-16 12:53:10.778: E/AndroidRuntime(32321):    at java.lang.reflect.Method.invokeNative(Native Method)
03-16 12:53:10.778: E/AndroidRuntime(32321):    at java.lang.reflect.Method.invoke(Method.java:511)
03-16 12:53:10.778: E/AndroidRuntime(32321):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
03-16 12:53:10.778: E/AndroidRuntime(32321):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
03-16 12:53:10.778: E/AndroidRuntime(32321):    at dalvik.system.NativeStart.main(Native Method)
03-16 12:53:10.778: E/AndroidRuntime(32321): Caused by: java.lang.NullPointerException
03-16 12:53:10.778: E/AndroidRuntime(32321):    at info.androidhive.googlemapsv2.MainActivity.onCreate(MainActivity.java:21)
03-16 12:53:10.778: E/AndroidRuntime(32321):    at android.app.Activity.performCreate(Activity.java:5066)
03-16 12:53:10.778: E/AndroidRuntime(32321):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
03-16 12:53:10.778: E/AndroidRuntime(32321):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
03-16 12:53:10.778: E/AndroidRuntime(32321):    ... 11 more
03-16 12:53:12.858: D/Process(32321): killProcess, pid=32321
03-16 12:53:12.858: D/Process(32321): dalvik.system.VMStack.getThreadStackTrace(Native Method)
03-16 12:53:12.858: D/Process(32321): java.lang.Thread.getStackTrace(Thread.java:599)
03-16 12:53:12.858: D/Process(32321): android.os.Process.killProcess(Process.java:956)
03-16 12:53:12.858: D/Process(32321): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:108)
03-16 12:53:12.858: D/Process(32321): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
03-16 12:53:12.858: D/Process(32321): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
03-16 12:53:12.858: D/Process(32321): dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

当您使用SupportMapFragment时,您应该使用

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

而不是

GoogleMap map = ((MapFragment) getFragmentManager()
        .findFragmentById(R.id.map)).getMap();