0

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

onCreate 中的日志语句没有显示输出,跟踪显示类在 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");
}
4

2 回答 2

4

启动一个新的 Activity 不是通过实例化它 ( new XMLParsingExample();) 来完成的,而是有一个意图,例如:

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

看看这里

于 2012-04-10T20:25:06.173 回答
0

Binyamin Sharet 是正确的。

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

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

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

于 2012-04-10T20:30:45.047 回答