我正在尝试将两个整数从我的主页 Activity (纬度和经度)传递到第二个 Activity ,该 Activity 包含一个 Google map 实例,该实例将在提供的纬度和经度处放置一个标记。我的难题是,当我在 Map_Page Activity 中检索包时,我传递的整数始终为 0,这是 Null 时的默认值。有没有人看到任何明显的错误?
我将以下内容存储在按钮单击 OnClick 方法中。
Bundle dataBundle = new Bundle();
dataBundle.putInt("LatValue", 39485000);
dataBundle.putInt("LongValue", -80142777);
Intent myIntent = new Intent();
myIntent.setClassName("com.name.tlc", "com.name.tlc.map_page");
myIntent.putExtras(dataBundle);
startActivity(myIntent);
然后在我的 map_page Activity 中,我在 onCreate 中有以下内容来获取数据。
Bundle extras = getIntent().getExtras();
System.out.println("Get Intent done");
if(extras !=null)
{
System.out.println("Let's get the values");
int latValue = extras.getInt("latValue");
int longValue = extras.getInt("longValue");
System.out.println("latValue = " + latValue + " longValue = " + longValue);
}
最佳答案
极客,
在这种情况下您不需要使用 Bundle。
像这样做你的看跌期权......
Intent myIntent = new Intent();
myIntent.setClassName("com.name.tlc", "com.name.tlc.map_page");
myIntent.putExtra("LatValue", (int)39485000);
myIntent.putExtra("LongValue", (int)-80142777);
startActivity(myIntent);
然后你可以用...检索它们
Bundle extras = getIntent().getExtras();
int latValue = extras.getInt("LatValue");
int longValue = extras.getInt("LongValue");
关于android - 在 Android 中的 Activity 和 Intent 之间传递整数总是导致零/空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5424135/