无法获取数据的意图 - 安卓 [英] Cannot get Data from the intent - Android

查看:213
本文介绍了无法获取数据的意图 - 安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者,我试图让一个Intent计算器,意味着我通过意向额外增加的投入并启动新活动,计算输入。最后,我想接受()的结果使用onActivityResult。现在的问题是我已经设置了结果文本视图,文本视图检测为双值(显示0.0从默认文本)。我无法理解问题的所在,因为在logcat中没有错误,甚至。下面是我的code。当我preSS的按钮,它只是显示0.0。无论我有多少次点击。此外,我在练意图所以只是尝试了基本的之一。请帮帮我吧。

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();       
}

}

推荐答案

D 意图变量 onActivityResult()所以请尝试更改

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();

@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.

另外,我同意了Monad的评论。虽然它不应该造成一个问题,它似乎有点陌生,做到这一切对的onCreate(),你可能会更好一点了与一个按钮或一些用户输入。否则,实在没有必要单独活动,但你可以用一个函数来执行计算的第一个活动

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.

修改

从文档

返回了启动​​这一活动的意图。

Return the intent that started this activity.

因此​​这将不会返回意图从第二活动传入的setResult ()。它会返回一个意图开始这活动,你会在的onCreate使用()可能。在这种情况下,未启动活动,但带回堆栈的顶部时,第二个活动已完成和的setResult()之称。

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.

这篇关于无法获取数据的意图 - 安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆