Android 单击按钮后以片段形式显示地图

Android 单击按钮后以片段形式显示地图,android,google-maps,android-fragments,Android,Google Maps,Android Fragments,我试图在Android项目中使用谷歌地图。一个人的详细信息将显示在行中,同时单击一个按钮,该按钮将使用针对该人保存的纬度和经度显示位置。我使用片段来显示 活动\u show\u map.xml <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.c

我试图在Android项目中使用谷歌地图。一个人的详细信息将显示在行中,同时单击一个按钮,该按钮将使用针对该人保存的纬度和经度显示位置。我使用片段来显示

活动\u show\u map.xml

<?xml version="1.0" encoding="utf-8"?>

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.dioglgt.myapplication.ClinicFragment" />
现在,要在单击按钮时触发地图显示,我使用了以下代码:

  public void onShowLocationClick(View view) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.map,new ClinicFragment());
    transaction.addToBackStack(null);
    transaction.commit();
}
但我得到了一个错误:

java.lang.IllegalArgumentException: No view found for id 0x7f0e0087 (com.example.dioglgt.myapplication:id/map) for fragment ClinicFragment{9dd7da #0 id=0x7f0e0087}

请在这方面提供指导。

您正试图在id映射为is inside activity\u show\u map且设置为Clinic fragment的片段上添加ClinicFragment。尝试使用frameLayout并用按钮上的片段替换该frameLayout,单击下面的链接:

Fragmentmanager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, new ClinicFragment()).commit();
在上面的代码中,
fragment_容器
是一个框架布局,它将被替换为you's
ClinicFragment

必须使用这个

 public void onShowLocationClick(View view) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(android.R.id.content,new ClinicFragment());
        transaction.addToBackStack(null);
        transaction.commit();
    }

transaction.add()
中的第一个参数是
片段将进入的
视图组的ID,而不是它自己的ID。尝试使用此代码
视图=充气器。充气(R.layout.activity\u show\u map,container,false)ClinicFragment
片段中的
onCreateView()
方法中的code>。为什么要投反对票?这难道不是我的努力,是不清楚还是没有用?在投入了大量的精力和时间之后,我把这个问题贴在了这里。也许否决这个问题显示了你的知识有多渊博。我真的不能代表其他人说话,但我们每周都会收到好几次这个问题-一个
碎片事务的ID是错误的。也许你因为缺乏研究而被否决,这是如果你将鼠标悬停在“否决票”箭头上列出的原因之一。但是,尽管进行了研究,我不可能没有抓住一个要点吗。如果一个人要从网络本身的研究中了解一切,就不需要像Stack Overflow这样的论坛。
 public void onShowLocationClick(View view) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(android.R.id.content,new ClinicFragment());
        transaction.addToBackStack(null);
        transaction.commit();
    }