Android代码中的Google地图给出空指针异常

时间:2014-01-25 04:26:09

标签: java android google-maps

我正在处理我需要谷歌地图的项目..我正在使用我之前的应用程序中使用的相同代码。但是这次我打开这个活动时会得到空指针异常。

这是我的map.java文件: -

  public class map extends  FragmentActivity{
 GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);


    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.map);

    Bundle b=getIntent().getExtras();
    String lat[]=b.getStringArray("lat");
    String log[]=b.getStringArray("log");
    String name[]=b.getStringArray("name");
    FragmentManager fmanager = getSupportFragmentManager();
    Fragment fragment = fmanager.findFragmentById(R.id.map);
    SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
    map = supportmapfragment.getMap();




    LatLng place[]=new  LatLng[lat.length];
    for(int i=0;i<place.length;i++)
    {
        if(lat[i].equals(""))
            lat[i]="0.0";
        if(log[i].equals(""))
            log[i]="0.0";
         place[i]=new  LatLng(Double.parseDouble(lat[i]),Double.parseDouble(log[i]));
    }
    Marker marker[]=new Marker[lat.length];
    for(int i=0;i<lat.length;i++)
    {
        marker[i] = map.addMarker(new MarkerOptions().position(place[i]).title(name[i]));
    }

        // Move the camera instantly to hamburg with a zoom of 15.
    for(int i=0;i<place.length;i++)
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(place[i], 15));
        // Zoom in, animating the camera.
      map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);


}



  }

我的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=".map" >

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

 </RelativeLayout>

我的宣言文件: -

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

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

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

       <uses-permission     android:name="com.pumaguard.pumaguard.locationapi.maps.permission.MAPS_RECEIVE" />
       <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/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.pumaguard.pumaguard.splash"
            android:label="@string/app_name"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar" >
        </activity>
        <activity
            android:name=".newactivity"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar" >
        </activity>

          <activity
            android:name=".map"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar" >
        </activity>

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

      </manifest>

lOGCAT错误: -

01-24 22:56:01.636: E/AndroidRuntime(835): FATAL EXCEPTION: main
01-24 22:56:01.636: E/AndroidRuntime(835): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pumaguard.pumaguard/com.pumaguard.pumaguard.map}: java.lang.NullPointerException
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.os.Looper.loop(Looper.java:137)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.app.ActivityThread.main(ActivityThread.java:5103)
01-24 22:56:01.636: E/AndroidRuntime(835):  at java.lang.reflect.Method.invokeNative(Native Method)
01-24 22:56:01.636: E/AndroidRuntime(835):  at java.lang.reflect.Method.invoke(Method.java:525)
01-24 22:56:01.636: E/AndroidRuntime(835):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-24 22:56:01.636: E/AndroidRuntime(835):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-24 22:56:01.636: E/AndroidRuntime(835):  at dalvik.system.NativeStart.main(Native Method)
01-24 22:56:01.636: E/AndroidRuntime(835): Caused by: java.lang.NullPointerException
01-24 22:56:01.636: E/AndroidRuntime(835):  at com.pumaguard.pumaguard.map.onCreate(map.java:62)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.app.Activity.performCreate(Activity.java:5133)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-24 22:56:01.636: E/AndroidRuntime(835):  ... 11 more

所以,这是我的代码请帮助我排除我的问题。 感谢。

1 个答案:

答案 0 :(得分:1)

首先不要向所有人显示您的API密钥

其次,您应该为每个应用生成新的API Key

第三,你可以使用

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

而不是

FragmentManager fmanager = getSupportFragmentManager();
Fragment fragment = fmanager.findFragmentById(R.id.map);
SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
map = supportmapfragment.getMap();

并告诉我们您收到错误的哪一行,请告诉我们该行。