2012-04-20 43 views
0

是否有可能将内容加载到一个活动的不同标签中?可以将内容加载到一个活动的不同标签中吗? Android标签

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

我想做一个位置跟踪应用程序。我已经设法让地图显示在一个标签中,但我尝试在另一个标签中加载其他内容,并且无法使其正常工作。我的应用程序崩溃。

/** 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); 


     } 

    } 
} 

}

+0

您提供的信息不足。崩溃?强制关闭?你能提供一些代码,崩溃和堆栈跟踪吗? – 2012-04-20 19:46:51

+0

它强制关闭 – sKwok12 2012-04-20 21:59:17

+0

mapview可以正常工作,但当我尝试将另一个活动运行到另一个选项卡时,它强制关闭... – sKwok12 2012-04-22 00:13:10

回答

0

是的,你可以做多TabViews无需使用TabActivities。请参阅this示例代码。