google map api v2 android - java.lang.NullPointerException

时间:2014-06-06 18:13:44

标签: java android google-maps nullpointerexception google-maps-android-api-2

我是Android编程新手。我正在开展一个项目,我必须从谷歌地图获取地图。没有发现eclipse的警告或错误代码。我不知道我的代码出了什么问题。请帮帮我

    06-07 09:39:29.357: E/AndroidRuntime(432): FATAL EXCEPTION: main
06-07 09:39:29.357: E/AndroidRuntime(432): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kulinerku/com.kulinerku.MainActivity}: java.lang.NullPointerException
06-07 09:39:29.357: E/AndroidRuntime(432):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
06-07 09:39:29.357: E/AndroidRuntime(432):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
06-07 09:39:29.357: E/AndroidRuntime(432):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
06-07 09:39:29.357: E/AndroidRuntime(432):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
06-07 09:39:29.357: E/AndroidRuntime(432):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 09:39:29.357: E/AndroidRuntime(432):  at android.os.Looper.loop(Looper.java:126)
06-07 09:39:29.357: E/AndroidRuntime(432):  at android.app.ActivityThread.main(ActivityThread.java:3997)
06-07 09:39:29.357: E/AndroidRuntime(432):  at java.lang.reflect.Method.invokeNative(Native Method)
06-07 09:39:29.357: E/AndroidRuntime(432):  at java.lang.reflect.Method.invoke(Method.java:491)
06-07 09:39:29.357: E/AndroidRuntime(432):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-07 09:39:29.357: E/AndroidRuntime(432):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-07 09:39:29.357: E/AndroidRuntime(432):  at dalvik.system.NativeStart.main(Native Method)
06-07 09:39:29.357: E/AndroidRuntime(432): Caused by: java.lang.NullPointerException
06-07 09:39:29.357: E/AndroidRuntime(432):  at com.kulinerku.MainActivity.onCreate(MainActivity.java:33)
06-07 09:39:29.357: E/AndroidRuntime(432):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
06-07 09:39:29.357: E/AndroidRuntime(432):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
06-07 09:39:29.357: E/AndroidRuntime(432):  ... 11 more

我的MainActivity.java

package com.kulinerku;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;

public class MainActivity extends FragmentActivity{
    final int RQS_GooglePlayService = 1;
    private GoogleMap myMap;

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

        FragmentManager myFragmentManager = getSupportFragmentManager();
        SupportMapFragment mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
        myMap = mySupportMapFragment.getMap();

        LatLng jogja = new LatLng(-7.782939, 110.367050);
        MarkerOptions markerJogja = new MarkerOptions();
        markerJogja.position(jogja);
        markerJogja.title("Tugu Jogja");
        markerJogja.snippet("Tugu Jogja adalah landmark Kota Jogja");

        myMap.setMapType(GoogleMap.MAP_TYPE_NONE);
        myMap.addMarker(markerJogja);
        myMap.getUiSettings().setCompassEnabled(true);
        myMap.getUiSettings().setZoomControlsEnabled(true);
        myMap.getUiSettings().setZoomGesturesEnabled(true);

        myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(jogja, 15));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

AndroidManifest

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

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

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



    <uses-permission android:name="com.kulinerku.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-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.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data 
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAEUM5FPpkLC0kPGMOnmyTSEwEcRNLjo7s"/>

        <activity 
            android:name="com.kulinerku.MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

    </application>

</manifest>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<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">

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


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

  

只有在加载基础地图系统并且片段中的基础视图存在时,才能使用getMap()获取GoogleMap。该类自动初始化地图系统和视图;但是,当它准备就绪时,您无法保证,因为这取决于Google Play服务APK的可用性。如果GoogleMap不可用,则getMap()将返回null。

来自http://developer.android.com/reference/com/google/android/gms/maps/SupportMapFragment.html

请务必在此处按照使用Google Play服务的所有步骤(包括导入项目等)进行操作:http://developer.android.com/google/play-services/setup.html