2017-04-02 39 views
1

我一直在试图窝我SupportMapFragment到另一个片段,但试图调用getMapAsync()我SupportMapFragment物体上时,我不断收到一个NullPointerException异常。我不知道如何解决这个问题,我一直在尝试一段时间:/SupportMapFragment#getMapAsync()对空对象引用

这是我的onCreateView方法从封闭的片段,应该包含地图。

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.activity_main, container, false); 

    CustomMap mapFrag = new CustomMap(); 
    FragmentTransaction transaction = this.getChildFragmentManager().beginTransaction(); 
    transaction.add(R.id.map_container, mapFrag).commit(); 

    SupportMapFragment smf = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map); 
    smf.getMapAsync(this); 

    return rootView; 

} 

下面是activity_main.xml中文件:

<android.support.percent.PercentRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".Activities.MainActivity" 
    android:focusableInTouchMode="true"> 

    <FrameLayout 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="0dp" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     app:layout_widthPercent="100%" 
     android:layout_marginBottom="250dp" 
     android:layout_centerInParent="true" 
     android:id="@+id/map_container" /> 

    <EditText 
     app:layout_widthPercent="90%" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:ems="10" 
     android:layout_alignParentBottom="true" 
     android:layout_alignLeft="@+id/map_container" 
     android:layout_alignStart="@+id/map_container" 
     android:layout_marginBottom="194dp" 
     android:id="@+id/editTextCode" 
     android:layout_toLeftOf="@+id/buttonCode" 
     android:layout_toStartOf="@+id/buttonCode" 
     android:hint="Enter Unique Code" /> 

    <Button 
     android:text="Submit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/editTextCode" 
     android:layout_alignRight="@+id/map" 
     android:layout_alignEnd="@+id/map" 
     android:id="@+id/buttonCode" /> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/buttonCode" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="15dp" 
     android:id="@+id/myListView" /> 

这里是我的CustomMap片段中使用的map_fragment.xml文件:

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

回答

2

尝试使用.newInstance来创建一个新的地图片段。下面是我的工作方式:

// Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    String MAP_FRAGMENT = "map_fragment"; 

    FragmentManager fragmentManager = getChildFragmentManager(); 
    SupportMapFragment mapFragment = (SupportMapFragment) fragmentManager 
      .findFragmentByTag(MAP_FRAGMENT); // Check if map already exists 

    if(mapFragment == null){ 
     // Create new Map instance if it doesn't exist 
     mapFragment = SupportMapFragment.newInstance(); 
     fragmentManager.beginTransaction() 
       .replace(R.id.map_container, mapFragment, MAP_FRAGMENT) 
       .commit(); 
    } 
    mapFragment.getMapAsync(this); 
+1

我欠你一杯啤酒,非常感谢你!现在似乎很简单! –