我想使用意圖從第一項活動向第二項活動傳遞2項信息(msgTotalCost & msgProfit),但不知道是什麼導致程序崩潰(不幸的是,應用程序已停止)。未能將信息從一項活動傳遞給另一項活動Android工作室
這裏是方法的在所述第一活動代碼(按下按鈕時將被激活):
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);
}
本地如下
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxx.calculator/com.xxxxx.calculator.ResultActivity}: java.lang.NullPointerException
又過早'findViewById'通話 – Selvin