I'm trying to pass a bundle of two values from a started class to my landnav app, but according to the debug nothing is getting passed, does anyone have any ideas why?
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;
public class PointEntry extends Activity{
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();
}
};
}
This is the class that is suppose to take the second point
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.MyLocationOverl开发者_JAVA技巧ay;
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;
}
}
};
}
You are not passing the Bundle you create. You have to do something like this:
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);
}
};
Edit: By the way, in this case you are creating a bundle that will die soon and that does not have too much data... so you can consider using putExtra(name, value)
method so that you can pass data directly to the Intent
. For example: intent.putExtra("latKey", latd);
精彩评论