[英]Android [SupportMapFragment] The method findViewById(int) is undefined for the type
public class PlaceMapsFragment extends SupportMapFragment {
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View v = inflater.inflate(R.layout.fragment_place_maps, container,
false);
setUpMapIfNeeded();
return v;
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((MapView) findViewById(R.id.map)).getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title(
"Marker"));
}
}
錯誤
The method findViewById(int) is undefined for the type PlaceMapsFragment
如果我沒記錯的話,“所有片段”都沒有本地的findViewById()
方法。 這就是為什么必須使用onCreateView()方法的原因。
這意味着您應該將setUpMapIfNeeded()
更改為setUpMapIfNeeded(View view)
然后在onCreateView()
中將v傳遞給修改后的方法。 您還需要更改mMap = ((MapView) findViewById(R.id.map)).getMap();
到mMap = ((MapView) view.findViewById(R.id.map)).getMap();
。
我不確定您可以SupportMapFragment
類。
要獲取SupportMapFragment
的新實例,請調用SupportMapFragment.newInstance
,這將返回您的對象。
如果將其子類化並調用,例如PlaceMapsFragment.newInstance()
您仍將返回一個SupportMapFragment
對象。
如果要以編程方式操作地圖,建議您執行以下操作。
this.mSupportMapFragment = SupportMapFragment.newInstance();
MapHandler mMapHandler = new mMapHandler(this.mSupportMapFragment.getMap());
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.