在Android中的活动和意图之间传递整数总是导致零/空

Gee*_*lat 6 eclipse android bundle android-intent android-activity

我正在尝试将两个整数从我的主页面活动(纬度和经度)传递到第二个活动,该活动包含一个谷歌地图实例,该实例将在纬度和长期提供标记.我的难题是当我在Map_Page活动中检索包时,我传递的整数总是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);
Run Code Online (Sandbox Code Playgroud)

然后在我的map_page活动中,我在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);

}
Run Code Online (Sandbox Code Playgroud)

Wil*_*ate 21

Geeklat,

在这种情况下,您不需要使用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);
Run Code Online (Sandbox Code Playgroud)

然后你可以用...检索它们

Bundle extras = getIntent().getExtras();
int latValue = extras.getInt("LatValue");
int longValue = extras.getInt("LongValue");
Run Code Online (Sandbox Code Playgroud)


Tar*_*sus 13

System.out.println("Let's get the values");
int latValue = extras.getInt("latValue");
int longValue = extras.getInt("longValue");
Run Code Online (Sandbox Code Playgroud)

不一样的

myIntent.putExtra("LatValue", (int)39485000);
myIntent.putExtra("LongValue", (int)-80142777);
Run Code Online (Sandbox Code Playgroud)

也可能是因为你没有在整个代码中保持Int的名称完全相同.Java和Android SDK区分大小写