Java Android studio中的简单计算器应用程序不断关闭
所以我一直在尝试在android studio上运行一个简单的计算器应用程序,但它总是崩溃。我已经检查了日志和所有的文件,但我一辈子都不知道出了什么问题。任何帮助都将不胜感激。 如果有帮助的话,我也可以在这里添加logcat! 这是我的XML代码: 我的java代码:Java Android studio中的简单计算器应用程序不断关闭,java,android,xml,logcat,Java,Android,Xml,Logcat,所以我一直在尝试在android studio上运行一个简单的计算器应用程序,但它总是崩溃。我已经检查了日志和所有的文件,但我一辈子都不知道出了什么问题。任何帮助都将不胜感激。 如果有帮助的话,我也可以在这里添加logcat! 这是我的XML代码: 我的java代码: public class Assignment_01 extends Activity { private Button btnGo; private EditText checkAmount, numOfPeo
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 ComponentInfo{com.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
好的,您正在为
onCreate()
中的checkamount
,checkAmountD
,numpopped
赋值,最初将为其赋值为空字符串
,因此在解析它时会抛出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
我也可以在这里添加logcat,如果这对没有日志的情况下有所帮助,我们将猜测如何解决问题,所以是的,您始终需要添加日志。这些是当发生冲突时,你应该首先考虑的事情problem@a_local_nobody好的,你会推荐用任何特定的东西来过滤它吗?你需要为你的应用程序找到错误日志,因为这些是你的崩溃,所以是的,在logcat中过滤错误和所选错误application@a_local_nobody是的,很有道理,就这么做了,谢谢我对你发布的堆栈跟踪做了一些更改,通常您应该查找E/AndroidRuntime:FATAL EXCEPTION:
或类似的内容,这样您就可以知道它从何处开始。在您的例子中,它的意思是:由:java.lang.NumberFormatException:empty String
引起。现在,如果您已经了解堆栈跟踪并读取它们,您可能已经猜到了代码中的错误,对吧:)它成功了!!!非常感谢:)