gpt4 book ai didi

java - fragment 中的 findFragmentById 不起作用

转载 作者:太空狗 更新时间:2023-10-29 16:28:33 25 4
gpt4 key购买 nike

您好,我有包含更多 fragment 的 viewpager。在第三个 View 上,我想放 map 。

我的 map 布局。(fragment_poloha ↓)

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:id="@+id/map2"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="paradox.galopshop.InfoItem" />
</FrameLayout>

我有这个类来设置网页浏览量的屏幕。

public class DemoFragment extends Fragment implements OnMapReadyCallback {


public static FragmentManager frag;
LayoutInflater inflater;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
int position = FragmentPagerItem.getPosition(getArguments());

switch (position){
case 0: return inflater.inflate(R.layout.fragment_strucne, container, false);
case 1: return inflater.inflate(R.layout.fragment_opis, container, false);
case 2: return inflater.inflate(R.layout.fragment_poloha, container, false);
case 3: return inflater.inflate(R.layout.fragment_demo2, container, false);
}
return inflater.inflate(R.layout.fragment_strucne, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
int position = FragmentPagerItem.getPosition(getArguments());

switch (position){
case 0: break;
case 1: break;
case 2:
// Get the SupportMapFragment and request notification
// when the map is ready to be used.
FragmentManager fm = getActivity().getSupportFragmentManager();
SupportMapFragment mapFragment = ((SupportMapFragment) fm.findFragmentById(R.id.map2));

// SupportMapFragment mapFragment = (SupportMapFragment) view.findViewById(R.id.map);
mapFragment.getMapAsync(this);

break;
case 3: break;
}
//TextView title = (TextView) view.findViewById(R.id.item_title);
//title.setText(String.valueOf(position*3));
}


/**
* Manipulates the map when it's available.
* The API invokes this callback when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user receives a prompt to install
* Play services inside the SupportMapFragment. The API invokes this method after the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Sydney, Australia,
// and move the map's camera to the same location.
LatLng sydney = new LatLng(48.3061, 18.0764);
googleMap.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Nitra"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sydney, 15.0f));
}


}

并且在 ViewCreated 应用程序崩溃,因为 mapFragment=null。但我不知道为什么它是空的。

framentmanager not null

请多多指教。我尝试了几个小时解决了我的问题,但没有成功

最佳答案

     @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
int position = FragmentPagerItem.getPosition(getArguments());

switch (position){
case 0: return inflater.inflate(R.layout.fragment_strucne, container, false);
case 1: return inflater.inflate(R.layout.fragment_opis, container, false);
case 2: View rootView =inflater.inflate(R.layout.fragment_poloha, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map2);
mapFragment.getMapAsync(this);
return rootView;
case 3: return inflater.inflate(R.layout.fragment_kontakt, container, false);
}
return inflater.inflate(R.layout.fragment_strucne, container, false);
}

关于java - fragment 中的 findFragmentById 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44519673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com