onCreate没有被主类调用

时间:2012-04-10 20:22:04

标签: android oncreate

我已经阅读了很多文章,但是没有一篇能解决我在XMLParsingExample类中没有调用onCreate方法的问题。

onCreate中的log-statement没有显示输出,并且跟踪显示该类在boolean finished = false之后退出,因此没有运行onCreate。

这里是代码:

public class MyMap extends MapActivity {

private MapView mapView;
private MapController mc;
private OverlayItem overlayItem;
private List<Overlay> mapOverlays;
private Drawable drawable;
private Drawable drawable2;
private MyItemizedOverlay itemizedOverlayMyLoc;
private MyItemizedOverlay itemizedOverlayRust;
private LocationManager locMgr;
private MyLocationListener locLstnr;XMLParsingExample mXMLParsingExample;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);

        mc = mapView.getController();
        mapView.setBuiltInZoomControls(true);

        locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locLstnr = new MyLocationListener();
        locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locLstnr);

        mapOverlays = mapView.getOverlays();

        // first overlay
        drawable = getResources().getDrawable(R.drawable.marker2);
        itemizedOverlayMyLoc = new MyItemizedOverlay(drawable, mapView);

        //                                  LAT                 LONG
        GeoPoint uwLoc = new GeoPoint((int)(52.22778*1E6),(int)(6.10428*1E6));
        overlayItem = new OverlayItem(uwLoc, "Uw locatie", "http://www.nu.nl");
        itemizedOverlayMyLoc.addOverlay(overlayItem);
        mapOverlays.add(itemizedOverlayMyLoc);

        // Rustpunten overlay
        drawable2 = getResources().getDrawable(R.drawable.rmarker3);
        itemizedOverlayRust = new MyItemizedOverlay(drawable2, mapView);

        mXMLParsingExample = new XMLParsingExample();

这里是没有调用oncreate的类:

public class XMLParsingExample extends Activity {

/** Create Object For SiteList Class */
public SitesList sitesList = null;
public ProgressDialog progressDialog;
boolean finished=false;

    /** Called when the activity is first created. */
    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.i("onCreate", "onCreate started");
}

2 个答案:

答案 0 :(得分:4)

启动新活动不是通过实例化它来实现的(new XMLParsingExample();),而是通过意图来实现,例如:

Intent intent = new Intent(this, XMLParsingExample.class);
startActivity(intent);

查看here

答案 1 :(得分:0)

Binyamin Sharet是正确的。

我认为你混淆了一个创建者方法,它在分配对象时会被调用,而onCreate()则是一个Android生命周期回调函数,在适当的时候由框架自动调用。

创建者函数通常不会在其名称中使用“创建”;它共享您要实例化其对象的类的名称。在您的情况下,创建者将被称为XMLParsingExample()

有关Android生命周期回调的详细信息,请参阅http://developer.android.com/guide/topics/fundamentals/activities.html