Android Studio 中的简单计算器应用程序不断关闭
Posted
技术标签:
【中文标题】Android Studio 中的简单计算器应用程序不断关闭【英文标题】:Simple calculator app in Android studio keeps closing 【发布时间】:2021-02-18 12:55:40 【问题描述】:所以我一直试图在 android studio 上运行一个简单的计算器应用程序,但它一直在崩溃。我已经检查了 logcat 和所有文件,但我仍然无法找出问题所在。任何帮助将非常感激。 如果有帮助的话,我也可以在这里添加 logcat! 这是我的 XML 代码:
我的java代码:
public class Assignment_01 extends Activity
private Button btnGo;
private EditText checkAmount, numOfPeople;
private double tip = 0.15;
double checkAmountD, checkAmountR;
double numPeopleD;
private TextView viewBill, viewPerson, viewTip;
double totalB, totalP, totalTP, totalT;
/** hi */
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main_linearlayout);
// get info from text box called checkAmount:
checkAmount = (EditText) findViewById(R.id.checkAmount);
String checkString = checkAmount.getText().toString();
checkAmountD = Double.parseDouble(checkString);
checkAmountR = Math.round(checkAmountD * 100.0) / 100.0;
// get info from number of people
numOfPeople = (EditText) findViewById(R.id.numOfPeople);
String numPeopleS = numOfPeople.getText().toString();
numPeopleD = Double.parseDouble(numPeopleS);
// if button is clicked, calculate other 4 values and display them
// button called btnLogin:
btnGo = (Button) findViewById(R.id.btnGo);
btnGo.setOnClickListener( new View.OnClickListener() // view. ? maybe not
// once button is clicked (onClick)
public void onClick(View view)
// calculate total tip, total bill, total per person, and total tip per person
totalT = checkAmountR * tip;
totalB = checkAmountR + totalT;
totalP = totalB / numPeopleD;
totalTP = totalT / numPeopleD;
// print these 4 out on the edit texts
setNewText();
// on click
); // on click listener
public void setNewText()
// print these 4 out on the edit texts
TextView viewBill = (TextView) findViewById(R.id.totalBill);
String totalBillS = String.valueOf(totalB);
viewBill.setText("$" + totalBillS);
viewBill.append("$" + totalBillS);
TextView viewPerson = (TextView) findViewById(R.id.totalPerPerson);
String totalPerPS = String.valueOf(totalP);
viewPerson.setText("$" + totalPerPS);
viewPerson.append("$" + totalPerPS);
TextView viewTip = (TextView) findViewById(R.id.totalTip);
String totalTipS = String.valueOf(totalP);
viewTip.setText("$" + totalTipS);
viewTip.append("$" + totalTipS);
TextView viewTipPerPerson = (TextView) findViewById(R.id.tipPerPerson);
String tipPerPersonS = String.valueOf(totalTP);
viewTipPerPerson.setText("$" + tipPerPersonS);
viewTipPerPerson.append("$" + tipPerPersonS);
堆栈溢出不允许我添加 XML 代码或 android 清单
提前谢谢你!
这是logcat的前半部分:
2021-02-18 00:27:56.993 3622-3622/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.course.example, PID: 3622
java.lang.RuntimeException: Unable to start activity ComponentInfocom.course.example/com.course.example.LayoutDemo: java.lang.NumberFormatException: empty String
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at com.course.example.LayoutDemo.onCreate(LayoutDemo.java:33)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2021-02-18 00:27:57.303 2605-3513/com.google.android.gms.persistent E/ActivityThread: Failed to find provider info for instantapp-dev-manager
2021-02-18 00:27:57.578 1988-2101/system_process E/SupplicantStaIfaceHal: ISupplicantStaIface.setPowerSave failed: .code = FAILURE_UNKNOWN, .debugMessage =
2021-02-18 00:27:57.601 1728-1953/? E/Netd: no such netId 101
2021-02-18 00:27:57.602 1988-2104/system_process E/ConnectivityService: Exception adding interface: java.lang.IllegalStateException: android.os.ServiceSpecificException: Machine is not on the network (code 64)
2021-02-18 00:27:57.602 1728-1953/? E/Netd: no such netId 101
2021-02-18 00:27:57.638 2605-3811/com.google.android.gms.persistent E/BluetoothAdapter: Bluetooth binder is null
2021-02-18 00:27:57.676 1988-2101/system_process E/SupplicantStaIfaceHal: ISupplicantStaIface.setPowerSave failed: .code = FAILURE_UNKNOWN, .debugMessage =
2021-02-18 00:27:57.679 1728-2197/? E/Netd: no such netId 101
2021-02-18 00:27:57.679 1988-2104/system_process E/ConnectivityService: Exception in addRoute for non-gateway: java.lang.IllegalStateException: android.os.ServiceSpecificException: Machine is not on the network (code 64)
2021-02-18 00:27:57.679 1728-2197/? E/Netd: no such netId 101
【问题讨论】:
I could add the logcat over here as well if that would help
没有日志,我们猜测如何解决问题,所以是的,您总是需要添加日志。当出现问题时,这些是您应该首先考虑的事情
@a_local_nobody 好的,你会建议用任何特定的东西过滤它吗?它的atm很长
您需要为您的应用找到错误日志,因为这些是您的崩溃,所以是的,在 logcat 中过滤错误并针对所选应用程序
@a_local_nobody 是的,有道理,就这么做了,谢谢
我已经对您发布的堆栈跟踪进行了一些更改,通常您应该寻找E/AndroidRuntime: FATAL EXCEPTION:
或类似的东西,这样您就会知道它从哪里开始。在你的情况下,它说:Caused by: java.lang.NumberFormatException: empty String
。现在,如果你知道堆栈跟踪并阅读它们,你可能已经猜到你的代码出了什么问题,对吧:)
【参考方案1】:
好吧,您在onCreate()
中为checkAmountR
、checkAmountD
、numPeopleD
分配值,最初将分配给空的String
,所以当您解析它时会抛出NumberFormatException
将它们移动到onClick()
中,所以当点击它时,它实际上会从EditText
获取值
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main_linearlayout);
checkAmount = (EditText) findViewById(R.id.checkAmount);
numOfPeople = (EditText) findViewById(R.id.numOfPeople);
btnGo = (Button) findViewById(R.id.btnGo);
btnGo.setOnClickListener( new View.OnClickListener() // view. ? maybe
public void onClick(View view)
String checkString = checkAmount.getText().toString();
checkAmountD = Double.parseDouble(checkString);
checkAmountR = Math.round(checkAmountD * 100.0) / 100.0;
String numPeopleS = numOfPeople.getText().toString();
numPeopleD = Double.parseDouble(numPeopleS);
totalT = checkAmountR * tip;
totalB = checkAmountR + totalT;
totalP = totalB / numPeopleD;
totalTP = totalT / numPeopleD;
// print these 4 out on the edit texts
setNewText();
// on click
); // on click listener
另外,请始终使用tryParse
,以避免这种NumberFormatException
【讨论】:
以上是关于Android Studio 中的简单计算器应用程序不断关闭的主要内容,如果未能解决你的问题,请参考以下文章
如何在Android Studio中对包含数值的EditText字段执行基本计算
Android Studio创建AIDL文件并实现进程间通讯
在android studio中使用firebase实时数据库检索当前登录用户的数据