ユーザー入力 (EditTexts) のリストがあり、それらを使用して、TextView TotalNum に表示する合計を計算したいと考えています。
Logcat は、Expanse.java の 70 行目に何か問題があることを示しています。
TotalNum.setText("£" + exTotal);
エラーの原因であると思われるexTotalの2倍の数字を取り出してみましたが、各EditTextのすべての数字を入力した後に計算ボタンを押すと、「残念ながらアプリ名が機能しなくなりました」と表示されます。
私は何を間違っていますか?
public class Expense extends Activity {
double exTotal, exRent, exElect, exHeat, exNet, exMobile, exOther;
TextView Total, TotalNum, Rent, Elect, Heat, Net, Mobile, Other;
EditText eRent, eElect, eHeat, eNet, eMobile, eOther;
String eRentS, eElectS, eHeatS, eNetS, eMobileS, eOtherS;
Button cal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expense);
Rent = (TextView) findViewById(R.id.tvRent);
Elect = (TextView) findViewById(R.id.tvElectricity);
Heat = (TextView) findViewById(R.id.tvHeating);
Net = (TextView) findViewById(R.id.tvInternet);
Mobile = (TextView) findViewById(R.id.tvMobile);
Other = (TextView) findViewById(R.id.tvOther);
Total = (TextView) findViewById(R.id.tvTotalExpense);
TotalNum = (TextView) findViewById(R.id.tvExpenseNum);
eRent = (EditText) findViewById(R.id.etRent);
eElect = (EditText) findViewById(R.id.etElectricity);
eHeat = (EditText) findViewById(R.id.etHeating);
eNet = (EditText) findViewById(R.id.etInternet);
eMobile = (EditText) findViewById(R.id.etMobile);
eOther = (EditText) findViewById(R.id.etOther);
cal = (Button) findViewById(R.id.bCal);
cal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
eRentS = eRent.getText().toString();
eElectS = eElect.getText().toString();
eHeatS = eHeat.getText().toString();
eNetS = eNet.getText().toString();
eMobileS = eMobile.getText().toString();
eOtherS = eOther.getText().toString();
exRent = Double.parseDouble(eRentS);
exElect = Double.parseDouble(eElectS);
exHeat = Double.parseDouble(eHeatS);
exNet = Double.parseDouble(eNetS);
exMobile = Double.parseDouble(eMobileS);
exOther = Double.parseDouble(eOtherS);
exTotal = exRent + exElect + exHeat + exNet + exMobile + exOther;
TotalNum.setText("£" + exTotal);
}
});
}
}
ログキャット
05-30 11:45:44.694: E/AndroidRuntime(775): FATAL EXCEPTION: main
05-30 11:45:44.694: E/AndroidRuntime(775): java.lang.NullPointerException
05-30 11:45:44.694: E/AndroidRuntime(775): at com.studentbudget.Expense$1.onClick(Expense.java:70)
05-30 11:45:44.694: E/AndroidRuntime(775): at android.view.View.performClick(View.java:4204)
05-30 11:45:44.694: E/AndroidRuntime(775): at android.view.View$PerformClick.run(View.java:17355)
05-30 11:45:44.694: E/AndroidRuntime(775): at android.os.Handler.handleCallback(Handler.java:725)
05-30 11:45:44.694: E/AndroidRuntime(775): at android.os.Handler.dispatchMessage(Handler.java:92)
05-30 11:45:44.694: E/AndroidRuntime(775): at android.os.Looper.loop(Looper.java:137)
05-30 11:45:44.694: E/AndroidRuntime(775): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-30 11:45:44.694: E/AndroidRuntime(775): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 11:45:44.694: E/AndroidRuntime(775): at java.lang.reflect.Method.invoke(Method.java:511)
05-30 11:45:44.694: E/AndroidRuntime(775): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-30 11:45:44.694: E/AndroidRuntime(775): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-30 11:45:44.694: E/AndroidRuntime(775): at dalvik.system.NativeStart.main(Native Method)