StartActivityForResutl,Bundle(Return data to ex-Activity)

=============================================

Download sourcefile Link: http://ishare.iask.sina.com.cn/f/33376951.html

Inclued files are:
ForResult.apk
ForResult.zip(source code)
ForResut.docx
result1.png
result2.png

================================================

Method: Use two layout: main.xml and result.xml

clip_image003clip_image005

Two Activity:

█ ForResult --gets the data user have input and store them into Intent/bundle which will be sent to next Activity.

         ○We override the method onActivityResult()to receive data transported from ForResultBack to keep UI after the user have input.

█ForResultBack –Get the data stored in the intent and then use them to calculate the standard weight.

         ○We set a listener to the Back button, return a intent (data is stored in it. ) to ForResult to do the job of transporting data.

clip_image007

█Problems:

***when click Back button , the program crashed.

=>To solve it, insert many System.out.println() in the sourse code to locate where was wrong.

clip_image002

=>First I find in line

” rb1 = (RadioButton) findViewById(R.id.sexM); ”

The id sexM does not match in the layout.

After solve this problem ,the problem crashed again.

=>in the same method ,I realized the two line make NULLPOINTEREXCEPTION:

“ bundle = this.getIntent().getExtras();”

It make the object :

“ForResultBack.this.setResult(RESULT_OK,intent);”

Is null.

Modified to :

intent = this.getIntent();

bundle = intent.getExtras();

===================================

preview:

 

1package test.ForRestult;

2

3import android.app.Activity;

10

11public class ForResult extends Activity {

12    /** Called when the activity is first created. */

13    private EditText et;

14    private RadioButton rb1;

15    private RadioButton rb2;

16    @Override

17    public void onCreate(Bundle savedInstanceState) {

18        super.onCreate(savedInstanceState);

19        setContentView(R.layout.main);

20        Button btn = (Button)findViewById(R.id.calcBtn);

21        btn.setOnClickListener(new Button.OnClickListener(){

22            @Override

23            public void onClick(View v) {

24                /** Get Height. */

25                et = (EditText) findViewById(R.id.height);

26                Double height = Double.parseDouble(et.getText().toString());

27                /** Get Sex. */

28                String sex = "" ;

29                 rb1 = (RadioButton) findViewById(R.id.sexM);

30                 rb2 = (RadioButton) findViewById(R.id.sexF);

31                 System.out.println("A");

32                if(rb1.isChecked())

33                {

34                    sex = "Male" ;

35                }

36                else

37                {

38                    sex = "Female" ;

39                }

40                System.out.println("B");

41

42                /**  create a new Intent Object ,and then assign its class.

*/

43                Intent intent = new Intent();

44                intent.setClass(ForResult.this, ForResultBack.class);

45                /**  Creat a new Bundle Object , and then put the data you want to put in it. */

46                Bundle bundle = new Bundle();

47                bundle.putDouble("height", height);

48                bundle.putString("sex", sex);

49                /**  assign the Bundle Object to Intent Object.  */

50                intent.putExtras(bundle);

51                /** Call Activity ForResultBack */

52                startActivityForResult(intent,0);

53                System.out.println("C");

54            }

55        });

56    }

57


 

58    @Override

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

60        // TODO Auto-generated method stub

61        System.out.println("D");

62        switch(resultCode)

63        {

64        case RESULT_OK :

65            /* Get the data from Activity ForResultBack , and apply the data to Activity ForResult*/

66            System.out.println("M");

67            Bundle bundle = data.getExtras();

68            System.out.println("X");

69            String sex = bundle.getString("sex");

70            System.out.println("Y");

71            Double height = bundle.getDouble("height");

72            System.out.println("Z");

73            et.setText("" + height);

74            System.out.println("N");

75            if(sex.equals("Male"))

76            {

77                rb1.setChecked(true);

78            }

79            else

80            {

81                 rb2.setChecked(true);

82            }

83            System.out.println("P");

84            break;

85

 

default:

86

 

System.out.println("Q");

87

 

break;

88

 

}

89

 

System.out.println("R");

90

}

 

91

 

 

92}

 

 

 

At last, it run normally.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值