Mapfragment findFragmentById总是空 [英] Mapfragment findFragmentById always null
本文介绍了Mapfragment findFragmentById总是空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有问题进入地图的片段。 getFragmentManager()。findFragmentById(R.id.map))总是返回空值。 我不知道为什么。 这是什么问题?
感谢您!
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /根
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的TextView
机器人:ID =@ + ID / tvNombreCentro
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:paddingBottom会=10dp
机器人:文本=大文本
机器人:textAppearance =:/>中的Android ATTR / textAppearanceLarge?
<片段
机器人:ID =@ + ID /图
机器人:名称=com.google.android.gms.maps.SupportMapFragment
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:标签=tag_fragment_map/>
< / LinearLayout中>
而在我的setContentView尝试进入地图后的活动,但我收到一个异常
公共类地图延伸活动{
私人GoogleMap的MMAP;
民营动作条AB;
私人TextView的tvNombreCentro;
私人TextView的tvTelefonoValor;
私人TextView的tvEMailValor;
私人TextView的tvWebValor;
私人TextView的tvDireccionValor;
@覆盖
公共无效的onCreate(包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);
地图= mapFrag.getMap();
I have problems to access the fragment of the map. getFragmentManager().findFragmentById(R.id.map)) returns always null. I don't know why. What's the problem?
Thank you!
<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>
And in the activity after the setContentView I try to access to the map, but I receive an exception
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();
}
解决方案
use this way:
SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
map = mapFrag.getMap();
here is full code with sample:
这篇关于Mapfragment findFragmentById总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文