我正在尝试将两个EditText相乘,包括editHours和editWage,并在TextView(粗略)中显示结果,非常感谢任何帮助
public class PayrollActivity extends Activity implements OnClickListener {
TextView gross, net, calculated;
EditText editHours, editWage;
int hours, wage, intGross, intNet, pay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payroll);
gross = (TextView) findViewById(R.id.gross);
hours= Integer.parseInt(editHours.getText().toString());
wage = Integer.parseInt(editWage.getText().toString());
gross.setText(hours*wage);
net = (TextView) findViewById(R.id.net);
calculated = (TextView) findViewById(R.id.calculated);
((Button) findViewById(R.id.addButton)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
StringBuilder str = new StringBuilder("It worked");
calculated.setText(str);
}
});
}
该应用程序崩溃
hours= Integer.parseInt(editHours.getText().toString());
logcat的
05-26 14:08:36.591: E/AndroidRuntime(1659): FATAL EXCEPTION: main
05-26 14:08:36.591: E/AndroidRuntime(1659): Process: co.mytms.payroll, PID: 1659
05-26 14:08:36.591: E/AndroidRuntime(1659): java.lang.RuntimeException: Unable to start activity ComponentInfo{co.mytms.payroll/co.mytms.payroll.PayrollActivity}: java.lang.NullPointerException
05-26 14:08:36.591: E/AndroidRuntime(1659): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-26 14:08:36.591: E/AndroidRuntime(1659): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-26 14:08:36.591: E/AndroidRuntime(1659): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-26 14:08:36.591: E/AndroidRuntime(1659): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-26 14:08:36.591: E/AndroidRuntime(1659): at android.os.Handler.dispatchMessage(Handler.java:102)
05-26 14:08:36.591: E/AndroidRuntime(1659): at android.os.Looper.loop(Looper.java:136)
05-26 14:08:36.591: E/AndroidRuntime(1659): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-26 14:08:36.591: E/AndroidRuntime(1659): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 14:08:36.591: E/AndroidRuntime(1659): at java.lang.reflect.Method.invoke(Method.java:515)
05-26 14:08:36.591: E/AndroidRuntime(1659): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-26 14:08:36.591: E/AndroidRuntime(1659): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-26 14:08:36.591: E/AndroidRuntime(1659): at dalvik.system.NativeStart.main(Native Method)
05-26 14:08:36.591: E/AndroidRuntime(1659): Caused by: java.lang.NullPointerException
05-26 14:08:36.591: E/AndroidRuntime(1659): at co.mytms.payroll.PayrollActivity.onCreate(PayrollActivity.java:22)
05-26 14:08:36.591: E/AndroidRuntime(1659): at android.app.Activity.performCreate(Activity.java:5231)
05-26 14:08:36.591: E/AndroidRuntime(1659): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-26 14:08:36.591: E/AndroidRuntime(1659): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-26 14:08:36.591: E/AndroidRuntime(1659): ... 11 more
答案 0 :(得分:2)
有几个问题。
首先在getText()
EditText
上调用onCreate
方法时,它们仍为空(未初始化,请参阅@Manishika answer)。因此Integer.parseInt
会尝试解析空String
。将其移动到按钮的onClick
方法中,这是您想要的行为(即,当您单击它时,它会将EditText
的两个值相乘,并将结果设置为{{1} })。
其次,在执行TextView
时,您将调用以gross.setText(hours*wage);
为参数的setText
方法。 Android系统会尝试加载其内容与您在int
文件中定义的ID相对应的适当String
。您需要调用接受strings.xml
作为参数的方法。 CharSequence
此外,在您的问题中添加堆栈跟踪总是很好,因为它可以帮助其他人更快地找到您的错误。
希望它有所帮助! :)
答案 1 :(得分:1)
在从他们那里获取文本之前,您还没有初始化EditTexts
editHours = (EditText)findViewById(R.id.editHours_id);
editWage = (EditText)findViewById(R.id.editWage_id);
并且还将你的计算放在onClick上,因为它最初只返回空字符串。
((Button) findViewById(R.id.addButton)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//If your input contains decimal values also then use BigDecimal()
//Following code will work for both int and decimal values
gross.setText(""+new BigDecimal(editHours.getText()
.toString()).multiply(new BigDecimal(editWage.getText()
.toString())));
StringBuilder str = new StringBuilder("It worked");
calculated.setText(str);
}
});