膨胀地图视图时出错

时间:2014-08-01 03:05:13

标签: java android android-fragments supportmapfragment

映射空点异常错误。在我的片段中加载地图时遇到问题。似乎总是为空。我尝试以不同的方式加载视图,但似乎仍然得到一个空图。

08-01 12:41:19.349: E/AndroidRuntime(25728): Process: com.ideamovinganddelivery, PID: 25728
08-01 12:41:19.349: E/AndroidRuntime(25728): java.lang.NullPointerException
08-01 12:41:19.349: E/AndroidRuntime(25728):    at com.idealmovinganddelivery.mapfragment.MapsFragment.setUpMapIfNeeded(MapsFragment.java:122)
08-01 12:41:19.349: E/AndroidRuntime(25728):    at com.idealmovinganddelivery.mapfragment.MapsFragment.onResume(MapsFragment.java:90)

主要活动

   public class MainActivity extends ActionBarActivity implements
        ActionBar.TabListener,MapDialogFragment.MapDialogListener{
        @Override
    public void onDialogPositiveClick(DialogFragment dialog, String Address) {
        MapsFragment mapFrag= (MapsFragment)     getSupportFragmentManager().findFragmentById(R.id.map);
         mapFrag.addDeliveryPoint(Address); 
    }

Fragment_map 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>

地图片段

       public class MapsFragment extends SupportMapFragment implements SearchView.OnQueryTextListener,
    OnMarkerClickListener
    {
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
            setHasOptionsMenu(true) ;


        view = inflater.inflate(R.layout.fragment_map, container,
                false);
            return view;
        }
public static 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) MainActivity.fragmentManager
                    .findFragmentById(R.id.map)).getMap();
            // Check if we were successful in obtaining the map.
            if (map != null)
                setUpMap();
        }

    }

1 个答案:

答案 0 :(得分:1)

错误是正确的。您无法在此处SupportMapFragment投放MapsFragment

在您执行此演员表时,您将找到ID为R.id.map的视图。您已在xml文件中将其定义为具有类型SupportMapFragment:

class="com.google.android.gms.maps.SupportMapFragment"

您应该定义<Fragment>元素以使用SupportMapFragment子类MapsFragment代替:

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