无法将数据返回到父活动

时间:2012-11-29 04:01:03

标签: android-intent

我正在尝试将数据(从网格中挑选的项目的位置)返回到父活动,但我的代码失败了。 debbuger显示“数据”在子活动中如何正确地处理“data.putExtra(”POS_ICON“,position)中的键和数据,但在父活动的onActivityResult()之后,debbuger显示没有键的”数据“也没有返回的数据......就像数据丢失其内容一样。我已经关注了其他帖子和教程,但没办法。请帮忙。

家长活动:

public void selIcono(View v){
    Intent intent = new Intent (this, SelIconoActivity.class);
    startActivityForResult(intent,PICK_ICON_REQUEST);
}

protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {    //here's the problem: no data is returned!!
    if (requestCode == PICK_ICON_REQUEST) {
        if (resultCode == RESULT_OK) {
            // An icon was picked. 
            putIcon(data.getIntExtra("POS_ICON", -1)); 
        }
    }
}

儿童活动:

public class SelIconoActivity extends Activity {    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sel_icono);

        GridView gridview = (GridView)findViewById(R.id.gr_iconos);
        gridview.setAdapter(new ImageAdapter (this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
           public void onItemClick(AdapterView<?> parent, View v, int position, long id) {                  
                Intent data = new Intent();
                data.putExtra("POS_ICON", position);                  
                setResult(Activity.RESULT_OK, data); 

                finish();          
            }
        });     
    }       
}

1 个答案:

答案 0 :(得分:2)

用此

替换您的代码putIcon(data.getIntExtra("POS_ICON", -1));
putIcon(data.getExtras().getInt("POS_ICON", -1));