Java getSupportFragmentManager().findFragmentById()始终返回null
我正试图在我的应用程序中显示谷歌地图,但在任务一开始我就遇到了问题。 该行:Java getSupportFragmentManager().findFragmentById()始终返回null,java,android,android-fragments,google-maps-android-api-2,Java,Android,Android Fragments,Google Maps Android Api 2,我正试图在我的应用程序中显示谷歌地图,但在任务一开始我就遇到了问题。 该行: SupportMapFragment MapFragm=(SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map) 始终返回null 我试过很多东西,但都不管用。你能帮我找到问题吗 这是我的密码: public class MapSectionFragment extends Fragment impl
SupportMapFragment MapFragm=(SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map)代码>
始终返回null
我试过很多东西,但都不管用。你能帮我找到问题吗
这是我的密码:
public class MapSectionFragment extends Fragment implements
ConnectionCallbacks,
OnConnectionFailedListener,
LocationListener,
OnMyLocationButtonClickListener,
OnMapReadyCallback{
private GoogleMap map;
Location location;
GoogleApiClient mGoogleApiClient;
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(5000) // 5 seconds
.setFastestInterval(16) // 16ms = 60fps
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
private void createMapView(){
try{
//here it comes
SupportMapFragment mapFrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
map = mapFrag.getMap(); //throws NullPointerException
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_map,
container, false);
createMapView();
return rootView;
}
@Override
public void onPause() {
super.onPause();
mGoogleApiClient.disconnect();
}
@Override
public void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient,
REQUEST,
this); // LocationListener
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onMapReady(GoogleMap googleMap) {
map.setMyLocationEnabled(true);
}
@Override
public boolean onMyLocationButtonClick() {
Toast.makeText(getActivity(), "MyLocation button clicked", Toast.LENGTH_SHORT).show();
// Return false so that we don't consume the event and the default behavior still occurs
// (the camera animates to the user's current position).
return false;
}
}
这是带有mapID的XML“片段\u节\u映射”:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/refresh_map_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="@string/refresh_map" />
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
谢谢你的帮助 请尝试此替换
SupportMapFragment mapFrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
与
因为
getChildFragmentManager()
将在片段中管理片段,因为您正在片段中使用SupportMapFragment,所以请尝试此解决方案
希望这将对您有所帮助请尝试此替换
SupportMapFragment mapFrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
与
因为
getChildFragmentManager()
将在片段中管理片段,因为您正在片段中使用SupportMapFragment,所以请尝试此解决方案
希望这将帮助您查看此地图片段的可能副本显然不在activitie的片段管理器中。。。显然至少有两个原因。。。第一:它是你片段的子片段。。。其次,onCreateView
没有在,嗯,onCreateView
内部完成。。。因此视图不会添加到活动中。。。所以地图片段没有添加到活动中。@Selvin我是新手,不太擅长这个。你能详细解释一下我能为我的问题做些什么吗?谢谢你必须学会。。。换句话说,不是在CreateView上使用不同的回调,而是在ActivityCreated或onStart上使用不同的回调。。。检查示例应用程序中的mapspossible副本,查看此地图片段显然不在activitie的片段管理器中。。。显然至少有两个原因。。。第一:它是你片段的子片段。。。其次,onCreateView
没有在,嗯,onCreateView
内部完成。。。因此视图不会添加到活动中。。。所以地图片段没有添加到活动中。@Selvin我是新手,不太擅长这个。你能详细解释一下我能为我的问题做些什么吗?谢谢你必须学会。。。换句话说,不是在CreateView上使用不同的回调,而是在ActivityCreated或onStart上使用不同的回调。。。查看示例应用程序mapsHappy,了解:)在30分钟的搜索后找到了此答案。非常感谢。很高兴听到:)在30分钟的搜索后找到了这个答案。非常感谢。