Android putExtra问题

时间:2011-04-02 04:21:53

标签: java android android-intent

我已经搜索过SE和谷歌,并发现了我认为如何与putExtra()一起实施getStringExtra()的正确例子。

我似乎无法解决的问题是我的putExtra数据似乎从来没有从目标活动中的getStringExtra调用中检索到。

我尝试了很多SE示例,其他人已经无数次地问过这个问题,但似乎从来没有让我更接近工作基础来扩展。

我的主要活动如下: (首先,我尝试了这个没有运气)

    // Click handler for group list items
    lvGroups.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            int gid       = groupIds.get(arg2);
            Intent intent = new Intent(RadSMS_Activity.this, RadSMS_CreateGroup.class);
            intent.putExtra("SELECTED_GROUP_ID", gid);
            startActivity(intent);
            finish();
        }
    });

(然后,我尝试了这个。也没有运气。)

    // Click handler for group list items
    lvGroups.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            int gid       = groupIds.get(arg2);
            Intent target = new Intent();
            target.putExtra("SELECTED_GROUP_ID", gid);
            Intent intent = new Intent(RadSMS_Activity.this, RadSMS_CreateGroup.class);
            startActivity(intent);
            finish();
        }
    });

我想从中提取值的目标活动如下:

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.creategroup);

        String strGID = getIntent().getStringExtra("SELECTED_GROUP_ID");
        selectedGID = new Long(strGID);
        // ... additional code would be here
    }
为了简洁起见,

功能被截断。

所以,根据我到目前为止看到的所有内容,看起来我做得对,但是当我在为selectedGID赋值的行放置一个断点时,strGID始终为null。这真的开始让我发疯。

有人可以告诉我,如果我做错了吗?

2 个答案:

答案 0 :(得分:3)

gid是一个int。 你正在投入一个int。 您似乎正在尝试检索字符串。 考虑:

int gid= getIntent().getIntExtra("SELECTED_GROUP_ID",-1);

答案 1 :(得分:1)

在将字符串作为字符串时输入整数值。它将始终返回null。使用intent.getExtras()。getInt()而不是intent.getStringExtra()。