我是Android开发的新手,我遇到了问题。
我创建了空活动项目,该项目成为主要项目。接下来我添加了第二项活动 - 谷歌地图。接下来,我使用<include/>
标记将地图活动包含在主要活动中:
<include
layout="@layout/activity_maps" />
当我启动我的应用程序时,会显示maps活动,但没有在自动生成的MapsActivity
类中调用的方法。它的代码是:
package com.example.work.myapplication3;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
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(this);
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setMessage("Message");
ad.create().show();
}
/**
* 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));
}
}
我添加了消息框以查看调用onCreate
方法的时间。但它从未调用,而地图显示在屏幕上。
我想创建几个活动,其自己的类包含其逻辑。但此时我可以创建子活动,但不能使用它的类。我希望在将此活动包含到另一个活动时自动创建活动的类。
我做错了什么?
由于