Cannot get Data from the intent - Android

2019-02-28 23:23发布

I am a beginner and I am trying to make an Intent Calculator, means I am add the inputs through Intent extras and starting a new activity for calculating the input. Finally I am trying to receive the result using onActivityResult(). The problem is i have set the result to text view, the text view detects it as a double value(displaying 0.0 from default text). I cant understand where the problem is as there are no errors in Logcat even. Below is my code. When I press the button it just displays 0.0. No matter how many times i click. Also I am practicing Intents so just tried the basic one. Kindly help me.

public class InputActivity extends Activity {


final int CALCULATION_COMPLETE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_input);

    final EditText num1 = (EditText) findViewById(R.id.num1_edit);
    final EditText num2 = (EditText) findViewById(R.id.num2_edit);

    Button add = (Button) findViewById(R.id.resultButton);

    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String  firstNum =  num1.getText().toString();
        String secondNum = num2.getText().toString();

        Intent resultIntent = new Intent(InputActivity.this,ResultActivity.class);
        resultIntent.putExtra("first", firstNum);
        resultIntent.putExtra("second", secondNum);
        startActivityForResult(resultIntent, CALCULATION_COMPLETE);
        }
    });     
}

@Override
protected void onActivityResult(int reqCode, int resCode, Intent d){
    super.onActivityResult(reqCode, resCode, d);
    TextView result = (TextView) findViewById(R.id.result);
    if(reqCode == CALCULATION_COMPLETE && resCode == RESULT_OK){

        Intent resultData = getIntent();
        Double addResult = resultData.getDoubleExtra("result", 0);

        String resultStr = addResult.toString();

        result.setText(resultStr);
    }
}

}

public class ResultActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    Intent calcIntent = getIntent();
    String firstStr = calcIntent.getStringExtra("first");
    String secondStr = calcIntent.getStringExtra("second");

    Double firstNum = Double.parseDouble(firstStr);
    Double secondNum = Double.parseDouble(secondStr);

    Double addResult = firstNum + secondNum;

    Intent result = new Intent();
    result.putExtra("result", addResult);

    setResult(RESULT_OK, result );
    finish();       
}

}

1条回答
叼着烟拽天下
2楼-- · 2019-02-28 23:38

d is your Intent variable in onActivityResult() so try changing

    @Override
protected void onActivityResult(int reqCode, int resCode, Intent d){
    super.onActivityResult(reqCode, resCode, d);
    TextView result = (TextView) findViewById(R.id.result);
    if(reqCode == CALCULATION_COMPLETE && resCode == RESULT_OK){

        Intent resultData = getIntent();

        Intent calcIntent = getIntent();

to

@Override
protected void onActivityResult(int reqCode, int resCode, Intent d){
    super.onActivityResult(reqCode, resCode, d);
    TextView result = (TextView) findViewById(R.id.result);
    if(reqCode == CALCULATION_COMPLETE && resCode == RESULT_OK){

        Intent resultData = d;  //This line here

If this doesn't fix your problem then please be a little more clear on what is/isn't happening.

Also, I agree with Monad's comment. While it shouldn't be causing a problem it does seem a little strange to do all of this on onCreate() and you may be a little better off with a Button or some user input. Otherwise, there really is no need for a separate Activity but you could use a function to do the calculation in the first Activity.

Edit

From the Docs

Return the intent that started this activity.

so this won't return the Intent passed from the second Activity in setResult(). It will return an Intent that started this Activity that you would use in onCreate() possibly. In this situation, the Activity isn't being started but brought back to the top of the stack when the second Activity is finished and setResult() is called.

查看更多
登录 后发表回答