我想在我的一個視圖中集成地圖視圖。android studio onMapReady不叫
我已經生成了一個新的地圖片段。它以不同的視角出現,像魅力一樣起作用。
然後,我嘗試將代碼集成到一個正常的活動中(一個帶有操作欄等等)。它有點作品 - 出現在屏幕上,但是MapReady在那個環境中永遠不會被調用(換句話說,我停留在非洲而不是去悉尼)。
一直在好幾個小時......
下面是XML代碼:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<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.jp.artfoundui.MapsActivity" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/monument_capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_menu_camera" />
這裏是Java代碼:
public class MonumentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_monument);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(MapsActivity.this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
}
有什麼建議嗎?
我知道這聽起來愚蠢,但你真的使用調試器,看看它是否步到onMapReady()?我正在查看我的一些個人代碼,我們的工作基本匹配。也許在MapsReady裏面有什麼不起作用? –
不錯的建議。我查了一下,沒有打電話給onMapReady。 – jpberry
但是當我使用調試器時,我注意到onCreate也沒有被調用。自然,onMapReady永遠不會被註冊。 所以,現在,爲什麼不onCreate被調用。我檢查了另一個演示代碼(在屏幕上截取片段的標準示例),並且onCreate確實被調用。但是,一旦我把片段放在我的活動中(上面的代碼),onCreate就不會被調用... – jpberry