我正在編寫一個有兩個輸出(氣體總量和總加侖總成本)的氣體計算器;我怎樣才能在totalCost
之前放置一個「$」,但不在totalGas
之前?
另外,當我在模擬器中運行程序,當我點擊重置按鈕強制關閉程序,我得到一個FATAL EXCEPTION: main
錯誤。
編輯:我想通了。我忘了在重置按鈕上導入myClickHandler。
這裏的java文件:
package com.example.gas;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import com.example.gas.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity
{
private EditText totalMiles;
private EditText price;
private EditText mpg;
private TextView totalGas;
private TextView totalCost;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
totalMiles = (EditText) findViewById(R.id.milesTxt);
totalCost = (EditText) findViewById(R.id.costTxt);
totalGas = (EditText) findViewById(R.id.gasTxt);
price = (EditText) findViewById(R.id.priceTxt);
mpg = (EditText) findViewById(R.id.mpgTxt);
}
public void myClickHandler(View view)
{
NumberFormat nf = new DecimalFormat("#,###.00");
switch (view.getId())
{
case R.id.calcBtn:
float inputMiles = Float.parseFloat(totalMiles.getText().toString());
float inputMPG = Float.parseFloat(mpg.getText().toString());
float inputPrice = Float.parseFloat(price.getText().toString());
float result1 = inputMiles/inputMPG;
String output1 = nf.format(result1);
totalGas.setText(output1);
float result = result1 * inputPrice;
String output = nf.format(result);
totalCost.setText(output);
break;
case R.id.resetBtn:
totalMiles.setText("");
totalCost.setText("");
totalGas.setText("");
price.setText("");
mpg.setText("");
break;
}
}
}
後的堆棧跟蹤。 – JoxTraex
你也可以在'activity_main.xml'中發佈XML佈局嗎? – Eric