gpt4 book ai didi

android - 无法从 Intent 中获取数据 - Android

转载 作者:行者123 更新时间:2023-11-29 21:34:55 25 4
gpt4 key购买 nike

我是初学者,我正在尝试制作一个 Intent Calculator,这意味着我要通过 Intent extras 添加输入并开始计算输入的新 Activity 。最后,我尝试使用 onActivityResult() 接收结果。问题是我已将结果设置为 TextView , TextView 将其检测为 double 值(从默认文本显示 0.0)。我无法理解问题出在哪里,因为 Logcat 甚至没有错误。下面是我的代码。当我按下按钮时,它只显示 0.0。无论我点击多少次。我也在练习 Intents,所以只尝试了基本的。请帮助我。

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() 中的 Intent 变量,因此请尝试更改

    @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

如果这不能解决您的问题,那么请更清楚地说明发生了什么/没有发生什么。

另外,我同意 Monad 的评论。虽然它不应该引起问题,但在 onCreate() 上执行所有这些操作似乎有点奇怪,使用 Button 或一些用户输入。否则,确实不需要单独的 Activity,但您可以使用函数在第一个 Activity 中进行计算。

编辑

From the Docs

Return the intent that started this activity.

因此这不会返回从 setResult() 中的第二个 Activity 传递的 Intent。它将返回一个 Intent,它启动了您可能会在 onCreate() 中使用的此 Activity。在这种情况下,Activity 没有启动,而是在第二个 Activity 完成并且 setResult() 被调用。

关于android - 无法从 Intent 中获取数据 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18599674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com