Android 计算器应用安装并打开,但当点击按钮组件时,应用自动关闭
Android calculator app installs and opens but when clicked on button component, App closes automatically
我制作了一个基本的存款金额计算应用程序,用户可以在其中输入金额和期限,然后单击计算按钮,然后应用程序应向用户显示利息值。如果我使用下面所说的代码在 android 工作室构建这个项目。应用程序在设备上安装并打开,但是当单击按钮时,应用程序将自动关闭。我不明白错误是在下面的代码中还是在设备中。
EditText et1,et2;
TextView mv,ie;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText)findViewById(R.id.et1);
et2=(EditText)findViewById(R.id.et2);
mv=(TextView)findViewById(R.id.mv);
ie=(TextView)findViewById(R.id.ie);
}
public void Cumlative(View v){
float depositamt = Integer.parseInt(et1.getText().toString());
float tenure = Integer.parseInt(et2.getText().toString());
float matval = (float)((depositamt*tenure)/100);
ie.setText((int) matval);
}
下面是xml文件
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Deposit Amount"
android:id="@+id/et1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Tenure in Months"
android:id="@+id/et2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cumlative"
android:textStyle="bold"
android:textColor="#8BC34A"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Maturity value"
android:id="@+id/mv"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Intrest earned"
android:id="@+id/ie"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_gravity="center"
android:id="@+id/cal"
android:onClick="Cumlative"/>
日志:
11-15 20:59:08.102 15074-15074/? I/fpc_tac: Returning from transfer command 0x24;3028
11-15 20:59:08.102 15074-15074/? I/fpc_tac: <--_fpc_tac_transfer_data returns tlTci->rsp.cmd_ret;3040
11-15 20:59:08.102 15074-15074/? I/fpc_tac: Enter _fpc_tac_load_database_id;2325
11-15 20:59:08.102 15074-15074/? I/fpc_tac: _fpc_tac_load_database_id, random_id_size, 96;2349
11-15 20:59:08.102 15074-15074/? I/fpc_tac: -->_fpc_tac_transfer_data command 0x29;2866
11-15 20:59:08.102 15074-15074/? I/fpc_tac: -->_fpc_tac_send_cmd command 0x02,0x29;2677
首先,请提供您 logcat 中显示的错误,以便我们轻松识别您的错误。
其次,错误在这两行:
float depositamt = Integer.parseInt(et1.getText().toString());
float tenure = Integer.parseInt(et2.getText().toString());
将它们更改为
float depositamt = Float.parseFloat(et1.getText().toString());
float tenure = Float.parseFloat(et2.getText().toString());
我制作了一个基本的存款金额计算应用程序,用户可以在其中输入金额和期限,然后单击计算按钮,然后应用程序应向用户显示利息值。如果我使用下面所说的代码在 android 工作室构建这个项目。应用程序在设备上安装并打开,但是当单击按钮时,应用程序将自动关闭。我不明白错误是在下面的代码中还是在设备中。
EditText et1,et2;
TextView mv,ie;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText)findViewById(R.id.et1);
et2=(EditText)findViewById(R.id.et2);
mv=(TextView)findViewById(R.id.mv);
ie=(TextView)findViewById(R.id.ie);
}
public void Cumlative(View v){
float depositamt = Integer.parseInt(et1.getText().toString());
float tenure = Integer.parseInt(et2.getText().toString());
float matval = (float)((depositamt*tenure)/100);
ie.setText((int) matval);
}
下面是xml文件
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Deposit Amount"
android:id="@+id/et1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Tenure in Months"
android:id="@+id/et2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cumlative"
android:textStyle="bold"
android:textColor="#8BC34A"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Maturity value"
android:id="@+id/mv"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Intrest earned"
android:id="@+id/ie"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_gravity="center"
android:id="@+id/cal"
android:onClick="Cumlative"/>
日志:
11-15 20:59:08.102 15074-15074/? I/fpc_tac: Returning from transfer command 0x24;3028
11-15 20:59:08.102 15074-15074/? I/fpc_tac: <--_fpc_tac_transfer_data returns tlTci->rsp.cmd_ret;3040
11-15 20:59:08.102 15074-15074/? I/fpc_tac: Enter _fpc_tac_load_database_id;2325
11-15 20:59:08.102 15074-15074/? I/fpc_tac: _fpc_tac_load_database_id, random_id_size, 96;2349
11-15 20:59:08.102 15074-15074/? I/fpc_tac: -->_fpc_tac_transfer_data command 0x29;2866
11-15 20:59:08.102 15074-15074/? I/fpc_tac: -->_fpc_tac_send_cmd command 0x02,0x29;2677
首先,请提供您 logcat 中显示的错误,以便我们轻松识别您的错误。 其次,错误在这两行:
float depositamt = Integer.parseInt(et1.getText().toString());
float tenure = Integer.parseInt(et2.getText().toString());
将它们更改为
float depositamt = Float.parseFloat(et1.getText().toString());
float tenure = Float.parseFloat(et2.getText().toString());