2015-10-29 103 views
1

Hotel_Location.xml如何訪問片段標籤到另一個活動的Android

<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。

我做錯了什麼。誰能幫幫我嗎。 在此先感謝

回答

0

您是否在AndroidManifest文件中設置了訪問權限? 除此之外,您必須指定從Google控制檯收到的Google Maps API密鑰才能訪問Google服務以顯示地圖。 您還需要谷歌谷歌控制檯上啓用了播放服務,然後包括谷歌播放服務的Android庫項目到您的項目..

下面是如何在Android項目中使用谷歌地圖一個很好的教程: http://javapapers.com/android/android-show-current-location-on-map-using-google-maps-api/

0

findFragmentById()方法是活動的成員,它試圖在其佈局容器內調用任何片段,而不是像findViewById()方法之外的任何片段。您的內容視圖是R.layout.activity_hotel_details,但您嘗試從R.layout.Hotel_Location訪問片段。

您可以初始化映射片段的實例並處理片段類中的片段操作。

相關問題