每当我调用getMap()时,我都会收到nullPointerException

时间:2014-05-06 03:52:27

标签: java android google-maps nullpointerexception

无论我做什么,我的代码似乎都在getMap()崩溃了。我试过几乎到处调整我的代码。我之前创建了这个应用程序,但由于计算机崩溃,我不得不重建。我的一些代码。现在它崩溃了。 我的主要代码:

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

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


public class FishFishSpotActivity extends Activity{

    public static final String XCORD = "xcord";
public static final String YCORD = "ycord";
public static final String NOTE = "note";
public static final String SAVED = "saved";
public static final String ID = "id";
TextView XCord= null;
TextView YCord= null;
TextView Note= null;
int Id = 0;
Boolean Saved = null;
private GoogleMap map;

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

    initializeMap();

    }

    protected void onStart(){
        map.isMyLocationEnabled();


    }

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

    }

    public boolean onCreateOptionsMenu(Menu menu){

        return false;

    }

    public void onMapClick(LatLng point){
        map.addMarker(new MarkerOptions().position(point));
    }

    public void onMarkerClick(Marker marker){

    }

    public static void SpotSaved(boolean saved, String A){

    }

    private void initializeMap() 
    {
        if (map == null) {
            map = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or not
            if (map == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }   

}

这是我的工作人员:

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

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


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

<uses-permission android:name="com.Nick.fishfishspot.permission.MAPS_RECEIVE"/>
<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"/>
<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.maps.v2.API_KEY"
        android:value="key is here"/>
   <meta-data
       android:name="com.google.android.gms.version"
       android:value="@integer/google_play_services_version" />

        <activity
            android:name="com.Nick.fishfishspot.FishFishSpotActivity"
            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>

这是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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.Nick.fishfishspot.FishFishSpotActivity$PlaceholderFragment" >

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

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="go" />

<EditText
    android:id="@+id/x_cord"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/y_cord"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/x_cord"
    android:layout_alignBottom="@+id/x_cord"
    android:layout_toLeftOf="@id/button1"
    android:layout_toRightOf="@+id/x_cord"
    android:ems="10" />

这是错误:

 05-05 22:44:53.390: E/AndroidRuntime(24604): FATAL EXCEPTION: main
    05-05 22:44:53.390: E/AndroidRuntime(24604): java.lang.RuntimeException: Unable to start activity         ComponentInfo{com.Nick.fishfishspot/com.Nick.fishfishspot.FishFishSpotActivity}:java.lang.NullPointerException
   05-05 22:44:53.390: E/AndroidRuntime(24604):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2304)
   05-05 22:44:53.390: E/AndroidRuntime(24604):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
   05-05 22:44:53.390: E/AndroidRuntime(24604):     at android.app.ActivityThread.access$700(ActivityThread.java:165)
   05-05 22:44:53.390: E/AndroidRuntime(24604):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
   05-05 22:44:53.390: E/AndroidRuntime(24604):     at android.os.Handler.dispatchMessage(Handler.java:99)
   05-05 22:44:53.390: E/AndroidRuntime(24604):     at android.os.Looper.loop(Looper.java:137)
   05-05 22:44:53.390: E/AndroidRuntime(24604):     at android.app.ActivityThread.main(ActivityThread.java:5450)
   05-05 22:44:53.390: E/AndroidRuntime(24604):     at java.lang.reflect.Method.invokeNative(Native Method)
   05-05 22:44:53.390: E/AndroidRuntime(24604):     at java.lang.reflect.Method.invoke(Method.java:525)
   05-05 22:44:53.390: E/AndroidRuntime(24604):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
   05-05 22:44:53.390: E/AndroidRuntime(24604):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
    05-05 22:44:53.390: E/AndroidRuntime(24604):    at dalvik.system.NativeStart.main(Native Method)
    05-05 22:44:53.390: E/AndroidRuntime(24604): Caused by: java.lang.NullPointerException
    05-05 22:44:53.390: E/AndroidRuntime(24604):    at com.Nick.fishfishspot.FishFishSpotActivity.initializeMap(FishFishSpotActivity.java:76)
    05-05 22:44:53.390: E/AndroidRuntime(24604):    at com.Nick.fishfishspot.FishFishSpotActivity.onCreate(FishFishSpotActivity.java:38)
    05-05 22:44:53.390: E/AndroidRuntime(24604):    at android.app.Activity.performCreate(Activity.java:5369)
    05-05 22:44:53.390: E/AndroidRuntime(24604):    at    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
    05-05 22:44:53.390: E/AndroidRuntime(24604):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
    05-05 22:44:53.390: E/AndroidRuntime(24604):    ... 11 more

1 个答案:

答案 0 :(得分:2)

你错过了

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

在清单

的应用程序标签中

getMap()可以返回null。您可以在初始化GoogleMap对象

之前检查Google Play服务的可用性
  

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

tools:context="com.Nick.fishfishspot.FishFishSpotActivity$PlaceholderFragment"同时提示您MapFragment在您在Activity中初始化时属于片段。

如果您想要片段内的地图,则需要在片段布局中扩展MapFragment或使用MapView

Android - android.view.InflateException: Binary XML file line #8: Error inflating class fragment