我正在尝试将数据从一个页面传输到另一个页面,但下面的一些问题是代码。我试图使用捆绑从第一个活动转移到第二个变量的值,但有些错误请告诉我哪里出错了。
以下是第一项活动: -
package route.planning;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class login extends Activity {
/** Called when the activity is first created. */
Context mCtx;
final static int START =0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mCtx = this;
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
// @Override
public void onClick(View v)
{
String fromLat = new String();
String fromLong = new String();
String toLat = new String();
String toLong = new String();
fromLat=String.valueOf(R.id.FromLatitude);
fromLong=String.valueOf(R.id.FromLongitude);
toLat=String.valueOf(R.id.ToLatitude);
toLong=String.valueOf(R.id.ToLongitude);
Intent intent = new Intent(mCtx, MapRouteActivity.class);
/*Sending some arguments*/
Bundle bundle = new Bundle();
bundle.putString("fromLat",fromLat );
bundle.putString("fromLong",fromLong );
bundle.putString("toLat",toLat );
bundle.putString("toLong",toLong );
intent.putExtras(bundle);
/*Start Activity*/
mCtx.startActivity(intent);
/*Start ActivityForResult*/
((Activity)mCtx).startActivityForResult(intent, 2);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == START)
{
Toast.makeText(mCtx, Integer.toString(resultCode), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onDestroy(){
super.onDestroy();
finish();
}
}
以下是我试图从上面获取价值的第二项活动的一部分
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import org.ci.geo.route.Road;
import org.ci.geo.route.RoadProvider;
import route.planning.R;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.widget.LinearLayout;
import android.widget.TextView;
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.Overlay;
public class MapRouteActivity extends MapActivity {
LinearLayout linearLayout;
MapView mapView;
private Road mRoad;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.routeplanning);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
Bundle extras = this.getIntent().getExtras();
final String fLat=extras.getString("fromLat");
final String fLong=extras.getString("fromLong");
final String tLat=extras.getString("toLat");
final String tLong=extras.getString("toLong");
new Thread() {
@Override
public void run() {
double fromLat=Double.parseDouble(fLat);
double fromLon=Double.parseDouble(fLong);
double toLat=Double.parseDouble(tLat);
double toLon=Double.parseDouble(tLong);
//double fromLat = 28.6353, fromLon = 77.2250, toLat = 30.7313, toLon = 76.7754;
/***url contains the path to fetch the kml file from the internet*/
String url = RoadProvider
.getUrl(fromLat, fromLon, toLat, toLon);
InputStream is = getConnection(url);
mRoad = RoadProvider.getRoute(is);
mHandler.sendEmptyMessage(0);
}
}.start();
}
答案 0 :(得分:5)
fromLat=String.valueOf(R.id.FromLatitude);
fromLong=String.valueOf(R.id.FromLongitude);
toLat=String.valueOf(R.id.ToLatitude);
toLong=String.valueOf(R.id.ToLongitude);
这是问题所在。您不会从活动中读取任何实际数据,而是获取控件标识符的字符串重新定位。
如果R.id.FromLatitude等是EditText的标识符,则需要获取用户输入:
fromLat = ((EditText)findViewById(R.id.FromLatitude)).getText().toString();
答案 1 :(得分:1)
您将无法从以下使用的代码中获取值:
fromLat=String.valueOf(R.id.FromLatitude);
fromLong=String.valueOf(R.id.FromLongitude);
toLat=String.valueOf(R.id.ToLatitude);
toLong=String.valueOf(R.id.ToLongitude);
这样,您将访问这些视图的ID,这些视图将在int中。
你必须得到视图的实例(EditText
,这里),然后必须提取它们的值。
使用@ Vladimir的解决方案,它是完美的。
答案 2 :(得分:1)
从第二项活动中删除最终关键字:
final String fLat=extras.getString("fromLat");
final String fLong=extras.getString("fromLong");
final String tLat=extras.getString("toLat");
final String tLong=extras.getString("toLong");
到
String fLat=extras.getString("fromLat");
String fLong=extras.getString("fromLong");
String tLat=extras.getString("toLat");
String tLong=extras.getString("toLong");