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" />
我欠你一杯啤酒,非常感謝你!現在似乎很簡單! –