gpt4 book ai didi

android - Mapfragment findFragmentById 始终为空

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:19:43 26 4
gpt4 key购买 nike

我在访问 map fragment 时遇到问题。getFragmentManager().findFragmentById(R.id.map)) 总是返回 null。我不知道为什么。有什么问题?

谢谢!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/tvNombreCentro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />


<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="tag_fragment_map" />

</LinearLayout>

在 setContentView 之后的 Activity 中,我尝试访问 map ,但收到异常

public class Mapa extends Activity {
private GoogleMap mMap;
private ActionBar ab;

private TextView tvNombreCentro;
private TextView tvTelefonoValor;
private TextView tvEMailValor;
private TextView tvWebValor;
private TextView tvDireccionValor;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.mapa_centro);
GoogleMap mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
}

最佳答案

这样使用:

SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);

map = mapFrag.getMap();

here is full code with sample:

关于android - Mapfragment findFragmentById 始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16967328/

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