2010-05-11 84 views
1

我試圖從啓動類傳遞一個包的兩個值到我的landnav應用程序,但根據調試沒有任何事情正在通過,有沒有人有任何想法爲什麼?爲什麼我的包沒有通過?

package edu.elon.cs.mobile; 

import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText;

公共類PointEntry延伸活動{

private Button calc; 
private EditText longi; 
private EditText lati; 
private double longid; 
private double latd; 


public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.pointentry); 

    calc = (Button) findViewById(R.id.coorCalcButton); 
    calc.setOnClickListener(landNavButtonListener); 

    longi = (EditText) findViewById(R.id.longitudeedit); 
    lati = (EditText) findViewById(R.id.latitudeedit); 
} 

private void startLandNav() { 

    Intent intent = new Intent(this, LandNav.class); 
    startActivityForResult(intent, 0); 
} 


private OnClickListener landNavButtonListener = new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
     Bundle bundle = new Bundle(); 
     bundle.putDouble("longKey", longid); 
     bundle.putDouble("latKey", latd); 
     longid = Double.parseDouble(longi.getText().toString()); 
     latd = Double.parseDouble(lati.getText().toString()); 
     startLandNav(); 
    } 


}; 

}

這是假設採取第二點

package edu.elon.cs.mobile; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 
import com.google.android.maps.MyLocationOverlay; 
import com.google.android.maps.Overlay; 

import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.EditText; 
import android.widget.TextView; 

public class LandNav extends MapActivity{ 
    private MapView map; 
    private MapController mc; 
    private GeoPoint myPos; 
    private SensorManager sensorMgr; 
    private TextView azimuthView; 

    private double longitudeFinal; 
    private double latitudeFinal; 
    double startTime; 
    double newTime; 
    double elapseTime; 
    private MyLocationOverlay me; 
    private Drawable marker; 
    private GeoPoint finalPos; 
    private SitesOverlay myOverlays; 

    public LandNav(){ 
     startTime = System.currentTimeMillis(); 


    } 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.landnav); 

     Bundle bundle = this.getIntent().getExtras(); 

     if(bundle != null){ 
      longitudeFinal = bundle.getDouble("longKey"); 
      latitudeFinal = bundle.getDouble("latKey"); 
     } 

     azimuthView = (TextView) findViewById(R.id.azimuthView); 
     map = (MapView) findViewById(R.id.map); 
     mc = map.getController(); 

     sensorMgr = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 

     LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     int longitude = (int)(location.getLongitude() * 1E6); 
     int latitude = (int)(location.getLatitude() * 1E6); 

     finalPos = new GeoPoint((int)(latitudeFinal*1E6), (int)(longitudeFinal*1E6)); 
     myPos = new GeoPoint(latitude, longitude); 
     map.setSatellite(true); 
     map.setBuiltInZoomControls(true); 
     mc.setZoom(16); 
     mc.setCenter(myPos); 

     marker = getResources().getDrawable(R.drawable.greenmarker); 
     marker.setBounds(0,0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight()); 

     me = new MyLocationOverlay(this, map); 
     myOverlays = new SitesOverlay(marker, myPos, finalPos); 

     map.getOverlays().add(myOverlays); 
    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     return false; 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     sensorMgr.registerListener(sensorListener, 
       sensorMgr.getDefaultSensor(Sensor.TYPE_ORIENTATION), 
       SensorManager.SENSOR_DELAY_UI); 

     me.enableCompass(); 
     me.enableMyLocation(); 
     //me.onLocationChanged(location) 

    } 

    protected void onPause(){ 
     super.onPause(); 
     me.disableCompass(); 
     me.disableMyLocation(); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     sensorMgr.unregisterListener(sensorListener); 
    } 

    private SensorEventListener sensorListener = new SensorEventListener() { 

     @Override 
     public void onAccuracyChanged(Sensor arg0, int arg1) { 
      // TODO Auto-generated method stub 
     } 

     private boolean reset = true; 

     @Override 
     public void onSensorChanged(SensorEvent event) { 
      newTime = System.currentTimeMillis(); 
      elapseTime = newTime - startTime; 
      if (event.sensor.getType() == Sensor.TYPE_ORIENTATION && elapseTime > 400) { 
       azimuthView.setText(Integer.toString((int) event.values[0])); 
       startTime = newTime; 
      } 
     } 
    }; 

} 
+0

有沒有例外?或者代碼在哪裏不起作用? – RoflcoptrException 2010-05-11 17:09:45

回答

4

你是不是通過你創建包的類。你必須做這樣的事情:

private void startLandNav(Bundle b) { 
    Intent intent = new Intent(this, LandNav.class); 
    intent.putExtras(b); 
    startActivityForResult(intent, 0); 
} 

private OnClickListener landNavButtonListener = new OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 
     Bundle bundle = new Bundle(); 
     bundle.putDouble("longKey", longid); 
     bundle.putDouble("latKey", latd); 
     longid = Double.parseDouble(longi.getText().toString()); 
     latd = Double.parseDouble(lati.getText().toString()); 
     startLandNav(bundle); 
    } 
}; 

編輯:順便說一句,在這種情況下,你要創建一個包,將不久於人世,並且沒有太多的數據...所以你可以考慮使用putExtra(name, value)方法,以便您可以將數據直接傳遞到Intent。例如:intent.putExtra("latKey", latd);

相關問題