简体   繁体   English

为什么 onActivityResult() 不起作用?

[英]Why doesn't onActivityResult() work?

What I am aiming to do here is to change the picture of "ibChamp" from the default one to "ahri".我在这里的目的是将“ibChamp”的图片从默认图片更改为“ahri”。 Note that the names inside the ***s are the names of the activity.请注意,***s 内的名称是活动的名称。

***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
 }

In this code, the onActivityResult() does not seem to run, since after "Champions.java" is finished, "ibChamp"'s picture did not change.在这段代码中,onActivityResult() 似乎没有运行,因为在“Champions.java”完成后,“ibChamp”的图片没有改变。 If there is something extremely obvious, please state it, and any questions are welcomed.如果有特别明显的地方,请说明,欢迎提出任何问题。

finish() will terminate the Activity. finish()将终止活动。 IMHO, it does not make sense to call finish() and then do layout changes (I ssume on the view in the Activity to be finished).恕我直言,调用finish()然后进行布局更改是没有意义的(我假设要完成的活动中的视图)。

Change "int data" into "Intent data":将“int 数据”更改为“意图数据”:

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

----> ---->

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM