如何在Android中访问Fragment标签到另一个活动

时间:2015-10-29 05:40:24

标签: android android-layout android-fragments

Hotel_Location.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

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

</RelativeLayout>

HotelLocation.java

public class HotelLocation extends Fragment {

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savaedInstanceState) {
        return inflater.inflate(R.layout.activity_hotel_location,container,false);
      }

}

HotelDetails.java

 public class HotelDetails extends AppCompatActivity {

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hotel_details);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Location"));
    tabLayout.addTab(tabLayout.newTab().setText("Direction"));
    tabLayout.setTabTextColors(getResources().getColorStateList(android.R.color.black));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final MapsPagerAdapter adapter = new MapsPagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() 
{
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });


        SupportMapFragment supportMapFragment =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMapLocation);

        googleMap = supportMapFragment.getMap();
        googleMap.setMyLocationEnabled(true);
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, true);
        location = locationManager.getLastKnownLocation(bestProvider);
        if (location != null) {
            onLocationChanged(location);
        }
        locationManager.requestLocationUpdates(bestProvider, 10000, 0,   
        this);

}
}

supportMapFragment在运行时返回null。

我做错了什么。谁能帮帮我吗。 提前致谢

2 个答案:

答案 0 :(得分:0)

您是否在AndroidManifest文件中设置了访问权限? 除此之外,您还必须指定从Google控制台收到的Google Maps API密钥才能访问Google服务以显示地图。 您还需要在谷歌控制台上启用谷歌播放服务,然后将谷歌播放服务作为Android库项目包含到您的项目中..

这是一个很好的教程,如何在Android项目中使用谷歌地图: http://javapapers.com/android/android-show-current-location-on-map-using-google-maps-api/

答案 1 :(得分:0)

findFragmentById()方法是活动的成员,它尝试调用其布局容器中的任何片段,而不是像findViewById()方法那样。您的内容视图是R.layout.activity_hotel_details,但您正在尝试从R.layout.Hotel_Location访问片段。

您可以初始化地图片段的实例并处理片段类中的片段操作。