为什么onActivityResult()不起作用?

时间:2014-03-25 02:02:34

标签: java android onactivityresult

我的目标是将“ibChamp”的图片从默认图片更改为“ahri”。请注意,***中的名称是活动的名称。

***CreateBuilds.java***
 ibChamp.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

            Intent champs = new Intent(CreateBuilds.this, Champions.class);
            //creates the Intent champs for startActivityForResult()
            startActivityForResult(champs, 0);
            //opens up Champions.class layout

        }
    });

 protected void onActivityResult(int requestCode, int resultCode, int data){
        //starts when this.finish() from Champions is ran
        ImageButton ibChamp = (ImageButton) findViewById(R.id.ibChamp);
        //creates the ImageButton ibChamp
        ibChamp.setImageResource(R.drawable.ahri);
        //sets the picture of ibChamp to "ahri"
};

***Champions.java****
private myapplicationtest mytestInst = new myapplicationtest();
    public void changePicture(final int champion){

    mytestInst.setInt(champion);
    //runs "setInt" from the myapplicationtest class
    this.finish();
    //closes this current layout to run onActivityResult
 }

在这段代码中,onActivityResult()似乎没有运行,因为在“Champions.java”完成后,“ibChamp”的图片没有改变。如果有一些非常明显的事情,请说出来,欢迎任何问题。

2 个答案:

答案 0 :(得分:1)

finish()将终止活动。恕我直言,调用finish()然后进行布局更改(我在要完成的活动中的视图上的ssume)是没有意义的。

答案 1 :(得分:1)

更改" int data"进入"意图数据":

protected void onActivityResult(int requestCode, int resultCode, int data)

---->

protected void onActivityResult(int requestCode, int resultCode, Intent data)