未能将信息从一个活动传递到另一个活动Android工作室

时间:2015-05-05 14:49:24

标签: android

我想使用Intent从第一个活动向第二个活动传递2个信息(msgTotalCost& msgProfit),但不确定是什么导致程序崩溃(不幸的是应用程序已经停止)。

以下是第一个活动中的方法代码(按下按钮时会激活):

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{THE_REQUEST} /(bar)/index\?op=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(bar)/(find)/?$ /$1/index.php?op=$2 [L,QSA]

额外键值位于第一个活动类

public void sendInput(View view) {
    Intent intent = new Intent(this, ResultActivity.class);
    EditText editTextPurPrice = (EditText) findViewById(R.id.editText_PP);
    EditText editTextSelPrice = (EditText) findViewById(R.id.editText_SP);
    EditText editTextStkQty = (EditText) findViewById(R.id.editText_QTY);
    EditText editTextLotSize = (EditText) findViewById(R.id.editText_LS);

    Double msgPurPrice = Double.parseDouble(editTextPurPrice.getText().toString());
    Double msgSelPrice = Double.parseDouble(editTextSelPrice.getText().toString());
    int msgStkQty = Integer.parseInt(editTextStkQty.getText().toString());
    int msgLotSize = Integer.parseInt(editTextLotSize.getText().toString());

    Double totalCost = getTotalCost(msgPurPrice, msgSelPrice, msgStkQty, msgLotSize);
    Double profit = getProfit(msgPurPrice, msgSelPrice, msgStkQty, totalCost);

    String msgTotalCost = String.valueOf(totalCost);
    String msgProfit = String.valueOf(profit);
    intent.putExtra(EXTRA_TOTALCOST, msgTotalCost);
    intent.putExtra(EXTRA_PROFIT, msgProfit);

    startActivity(intent);

}

以下是第二项活动的代码:

public final static String EXTRA_TOTALCOST = "com.xxxxx.calculator.InfoActivity.totalcost";
public final static String EXTRA_PROFIT = "com.xxxxx.calculator.InfoActivity.profit";

本地如下

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String totalCost = intent.getStringExtra(InfoActivity.EXTRA_TOTALCOST);
    String profit = intent.getStringExtra(InfoActivity.EXTRA_PROFIT);
    TextView costOutput = (TextView) findViewById(R.id.resultTTCost);
    TextView profitOutput = (TextView) findViewById(R.id.resultProfit);
    costOutput.setText(totalCost);
    profitOutput.setText(profit);
    setContentView(R.layout.activity_result);
}

2 个答案:

答案 0 :(得分:3)

更改

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String totalCost = intent.getStringExtra(InfoActivity.EXTRA_TOTALCOST);
    String profit = intent.getStringExtra(InfoActivity.EXTRA_PROFIT);
    TextView costOutput = (TextView) findViewById(R.id.resultTTCost);
    TextView profitOutput = (TextView) findViewById(R.id.resultProfit);
    costOutput.setText(totalCost);
    profitOutput.setText(profit);
    setContentView(R.layout.activity_result);
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
    Intent intent = getIntent();
    String totalCost = intent.getStringExtra(InfoActivity.EXTRA_TOTALCOST);
    String profit = intent.getStringExtra(InfoActivity.EXTRA_PROFIT);
    TextView costOutput = (TextView) findViewById(R.id.resultTTCost);
    TextView profitOutput = (TextView) findViewById(R.id.resultProfit);
    costOutput.setText(totalCost);
    profitOutput.setText(profit);
}

答案 1 :(得分:3)

这与意图和数据传输无关。您只需要{/ 1}}布局之前就可以找到setContentView的视图元素。