Android Studio 中的简单计算器应用不断关闭

问题描述

所以我一直在尝试在 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 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

解决方法

好吧,您正在为 checkAmountR 中的 checkAmountDnumPeopleDonCreate() 分配值,这些值最初将分配为空的 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