Android 可以将内容加载到一个活动的不同选项卡中吗?安卓标签

Android 可以将内容加载到一个活动的不同选项卡中吗?安卓标签,android,android-activity,tabs,maps,host,Android,Android Activity,Tabs,Maps,Host,是否可以将内容加载到一个活动的不同选项卡中 我见过很多例子,其他用户只是在单独的文件中加载活动。我试过这个方法,但不适合我 我正在尝试制作一个位置跟踪应用程序。我设法让地图显示在一个选项卡中,但我尝试在另一个选项卡中加载其他内容,但无法使其工作。我的应用程序崩溃了 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { sup

是否可以将内容加载到一个活动的不同选项卡中

我见过很多例子,其他用户只是在单独的文件中加载活动。我试过这个方法,但不适合我

我正在尝试制作一个位置跟踪应用程序。我设法让地图显示在一个选项卡中,但我尝试在另一个选项卡中加载其他内容,但无法使其工作。我的应用程序崩溃了

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    initMapView();
    initMyLocation();

    TabHost.TabSpec spec;

    TabHost th = (TabHost)findViewById(R.id.tabhost);
    th.setup();
    spec = th.newTabSpec("tag1");
    spec.setContent(R.id.mapTab);
    spec.setIndicator("Map");
    th.addTab(spec);

    spec = th.newTabSpec("tag2");
    spec.setContent(R.id.logTab);
    spec.setIndicator("Log");
    th.addTab(spec);

    spec = th.newTabSpec("tag3");
    spec.setContent(R.id.detailsTab);
    spec.setIndicator("Details");
    th.addTab(spec);

}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

//Zoom In/Out Controls
private void initMapView() {
    map = (MapView) findViewById(R.id.mvMain);
    controller = map.getController();
    map.setSatellite(true);
    //map.setStreetView(true);
    map.setBuiltInZoomControls(true);
}


//Creates an Overlay that marks current position
private void initMyLocation() {
    final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
    overlay.enableMyLocation();
    overlay.enableCompass();
    overlay.runOnFirstFix(new Runnable() {
        public void run() {
            controller.setZoom(17);
            controller.animateTo(overlay.getMyLocation());
            map.getOverlays().add(overlay);
        }

    });

}
//Experiment
public class detailsTab extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.id.detailsTab);

        LocationManager locationManager;
        String context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager)getSystemService(context);

        String provider = LocationManager.GPS_PROVIDER;
        Location location = locationManager.getLastKnownLocation(provider);
        updateWithNewLocation(location);
    }

    private void updateWithNewLocation(Location location) {
        String latLongString;
        TextView myLocationText;
        myLocationText = (TextView)findViewById(R.id.detailsText);
        if(location != null){
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            latLongString = "Lat:" + lat + "\nLong" + lng;
        }
        else {
            latLongString = "No Location Found";
        }
        myLocationText.setText("Your current position is: \n" + latLongString);
    }
}

public class NewOverlay extends Overlay {
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        Projection projection = mapView.getProjection();

        Double lat = lati *1E6;
        Double lng = longi *1E6;

        GeoPoint geoPoint = new GeoPoint(lat.intValue(), lng.intValue());

        if (shadow == false) {
            Point myPoint = new Point();
            projection.toPixels(geoPoint, myPoint);

            //Creating and setting up the paint brush
            Paint paint = new Paint();
            paint.setARGB(250, 255, 0, 0);
            paint.setAntiAlias(true);
            paint.setFakeBoldText(true);

            //Create circle
            int rad = 25;
            RectF oval = new RectF(myPoint.x-rad, myPoint.y-rad, myPoint.x+rad, myPoint.y+rad);

            canvas.drawOval(oval, paint);
            canvas.drawText("Red Circle", myPoint.x+rad, myPoint.y, paint);


        }

    }
}

}

是的,您可以在不使用TabActivities的情况下执行多个选项卡视图。请参阅示例代码。

您没有提供足够的信息。撞车?强行关闭?你能提供一些代码,它在哪里崩溃和堆栈跟踪?mapview工作正常,但当我尝试在另一个选项卡中运行另一个活动时,它会强制关闭。。。