zoukankan      html  css  js  c++  java
  • StartActivityForResutl,Bundle(Return data to exActivity)

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

    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.

  • 相关阅读:
    LR与SVM的异同
    精确率,召回率
    XgBoost推导与总结
    梯度下降中的步长选择-线性搜索
    页面去掉某个css属性
    composer 安装某个插件后 引入方法
    javascript,检测对象中是否存在某个属性
    js 计算字符串长度 中文为2 英文为1
    laravel 新手 =_= 持续更新
    php compact() 函数
  • 原文地址:https://www.cnblogs.com/xilifeng/p/2623074.html
Copyright © 2011-2022 走看看