Android Map始终为null

时间:2014-08-02 18:46:12

标签: java android google-maps

我尝试在片段中实现地图,但似乎方法getMap()始终返回null。我已尝试过stackoverflow上的其他解决方案,例如检查导入并确保我使用正确的xml类。这些都没有奏效。

该行

((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap()

总是抛出错误。

08-02 13:39:16.801: E/AndroidRuntime(16502): FATAL EXCEPTION: main
08-02 13:39:16.801: E/AndroidRuntime(16502): Process: com.ideamovinganddelivery, PID: 16502
08-02 13:39:16.801: E/AndroidRuntime(16502): java.lang.RuntimeException: Unable to resume activity {com.ideamovinganddelivery/com.idealmovinganddelivery.MainActivity}: java.lang.NullPointerException
08-02 13:39:16.801: E/AndroidRuntime(16502):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3321)
08-02 13:39:16.801: E/AndroidRuntime(16502):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3359)
08-02 13:39:16.801: E/AndroidRuntime(16502):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
08-02 13:39:16.801: E/AndroidRuntime(16502):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-02 13:39:16.801: E/AndroidRuntime(16502):    at android.os.Looper.loop(Looper.java:157)
08-02 13:39:16.801: E/AndroidRuntime(16502):    at android.app.ActivityThread.main(ActivityThread.java:5872)
08-02 13:39:16.801: E/AndroidRuntime(16502):    at java.lang.reflect.Method.invokeNative(Native Method)
08-02 13:39:16.801: E/AndroidRuntime(16502):    at java.lang.reflect.Method.invoke(Method.java:515)
08-02 13:39:16.801: E/AndroidRuntime(16502):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-02 13:39:16.801: E/AndroidRuntime(16502):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
08-02 13:39:16.801: E/AndroidRuntime(16502):    at dalvik.system.NativeStart.main(Native Method)
08-02 13:39:16.801: E/AndroidRuntime(16502): Caused by: java.lang.NullPointerException
08-02 13:39:16.801: E/AndroidRuntime(16502):    at com.idealmovinganddelivery.mapfragment.MapsFragment.setUpMapIfNeeded(MapsFragment.java:119)
08-02 13:39:16.801: E/AndroidRuntime(16502):    at com.idealmovinganddelivery.mapfragment.MapsFragment.onResume(MapsFragment.java:87)

MapFragment Xml加上类

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_map_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.idealmovinganddelivery.MainActivity$" >

     <fragment
         android:id="@+id/map"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"

         class="com.idealmovinganddelivery.mapfragment.MapsFragment" />

</RelativeLayout>
////////////

import android.annotation.SuppressLint;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Context;
import android.graphics.Color;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import com.ideamovinganddelivery.R;
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setHasOptionsMenu(true) ;
        View view=inflater.inflate(R.layout.fragment_map, container,false);


        return view;

    }

      public void onResume() {
         super.onResume();
         setUpMapIfNeeded();
         map.setOnMarkerClickListener(this);

         }
     public  void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (map == null) {
            // Try to obtain the map from the SupportMapFragment.
            map = ((SupportMapFragment) getFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
            // Check if we were successful in obtaining the map.
            if (map != null)
                setUpMap();
        }

    }

1 个答案:

答案 0 :(得分:0)

如果要为3.0以上的设备提供地图,请保持xml相同,但不要使用此行 -

map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

写下这一行 -

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

对于低于3.0的设备,您必须将xml更改为如下所示 -

<fragment
         android:id="@+id/map"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         class="com.idealmovinganddelivery.mapfragment.SupportMapFragment" />