我正在尝试将两个值从一个已启动的类传递到我的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扩展了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();
}
};
}
这是假设采取第二点的类
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 :(得分:4)
您没有通过您创建的Bundle。你必须做这样的事情:
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);