0

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

家长活动:

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();          
            }
        });     
    }       
}
4

1 回答 1

2

putIcon(data.getIntExtra("POS_ICON", -1));用这个 替换你的代码

putIcon(data.getExtras().getInt("POS_ICON", -1));
于 2012-11-29T17:02:49.773 回答