Android应用程序代码不起作用

时间:2016-12-19 01:42:00

标签: java android-studio

我在使用此应用程序时遇到了一些问题。一切看起来都不错,但是当我运行应用程序时,它会完美加载,直到我点击计算按钮并尝试让它思考。请给我一个关于修复什么或从哪里开始寻找的提示,因为我很难过!

问题:
1.开始屏幕要求您购买的洗车包装类型。

  1. 用户选择哪种类型的洗车 - 仅外部或外部内部真空服务。洗车应用程序收取8.99美元的外部清洗费用和12.99美元的外部清洗,内部真空吸尘器用于12个或更多洗车的包装。如果您选择少于12次洗涤,外部清洗的费用为10.99美元,内部吸尘器的外部费用为15.99美元。

  2. 选择CALCULATE PACKAGE按钮后,将显示购买洗车次数的总价格(图4-36)。

  3. 收到错误:

     E/AndroidRuntime: FATAL EXCEPTION: main
                          Process: net.androidbootcamp.carwashapp, PID: 3212
                          java.lang.IllegalArgumentException: Cannot format given Object as a Number
                              at java.text.DecimalFormat.format(DecimalFormat.java:593)
                              at java.text.Format.format(Format.java:157)
                              at net.androidbootcamp.carwashapp.MainActivity$1.onClick(MainActivity.java:53)
                              at android.view.View.performClick(View.java:5610)
                              at android.view.View$PerformClick.run(View.java:22265)
                              at android.os.Handler.handleCallback(Handler.java:751)
                              at android.os.Handler.dispatchMessage(Handler.java:95)
                              at android.os.Looper.loop(Looper.java:154)
                              at android.app.ActivityThread.main(ActivityThread.java:6077)
                              at java.lang.reflect.Method.invoke(Native Method)
                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
    

    Java代码:

    package net.androidbootcamp.carwashapp;
    
    import android.net.Uri;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RadioButton;
    import android.widget.TextView;
    import com.google.android.gms.common.api.GoogleApiClient;
    
    import java.text.DecimalFormat;
    
    public class MainActivity extends AppCompatActivity {
        double priceOne = 10.99;
        double discountOne = 8.99;
        double priceTwo = 15.99;
        double discountTwo = 12.99;
        double total;
        double washesEntered;
        /**
         * ATTENTION: This was auto-generated to implement the App Indexing API.
         * See https://g.co/AppIndexing/AndroidStudio for more information.
         */
        private GoogleApiClient client;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setLogo(R.mipmap.ic_launcher);
            getSupportActionBar().setDisplayUseLogoEnabled(true);
            final EditText numWashes = (EditText) findViewById(R.id.numOfWashes);
            final RadioButton exteriorOnly = (RadioButton) findViewById(R.id.radExteriorOnly);
            final RadioButton exteriorInterior = (RadioButton) findViewById(R.id.radExteriorInterior);
            final TextView result = (TextView) findViewById(R.id.txtResult);
            Button calc = (Button) findViewById(R.id.btnCalulate);
    
            calc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    washesEntered = Double.parseDouble(numWashes.getText().toString());
                    DecimalFormat tenth = new DecimalFormat("#.#");
                    if (exteriorOnly.isChecked() && washesEntered < 12) {
                        total = washesEntered * priceOne;
                        result.setText(tenth.format("$" + total + " for " + washesEntered + " washes"));
                    }
                    if (exteriorOnly.isChecked() && washesEntered > 12) {
                        total = washesEntered * discountOne;
                        result.setText(tenth.format("$" + total + " for " + washesEntered + " washes"));
                    }
                    if (exteriorInterior.isChecked() && washesEntered < 12) {
                        total = washesEntered * priceTwo;
                        result.setText(tenth.format("$" + total + " for " + washesEntered + " washes"));
                    }
                    if (exteriorInterior.isChecked() && washesEntered > 12) {
                        total = washesEntered * discountTwo;
                        result.setText(tenth.format("$" + total + " for " + washesEntered + " washes"));
                    }
    
                }
            });
    
    
        }
    
    
    }
    

    XML代码:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="net.androidbootcamp.carwashapp.MainActivity">
    
        <TextView
            android:text="@string/txtDiscount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txtDiscount"
            android:textSize="18sp"
            android:layout_below="@+id/txtTitle"
            android:layout_centerHorizontal="true" />
    
        <Button
            android:text="@string/btnCalculate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnCalulate"
            android:textSize="24sp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" />
    
    
        <TextView
            android:text="@string/txtTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txtTitle"
            android:textSize="36sp"
            android:textColor="#ffa500"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/carwash"
                android:id="@+id/imgCarWash"
                android:contentDescription="@string/imgDescription"
                android:layout_alignParentBottom="true"
                android:layout_alignParentStart="true" />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/txtResult"
                android:layout_marginTop="46dp"
                android:layout_alignTop="@+id/imgCarWash"
                android:layout_alignEnd="@+id/txtTitle"
                android:textSize="18sp"
                android:textColor="#0000ff"
                android:textAlignment="center" />
    
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginTop="18dp"
            android:layout_below="@+id/txtDiscount"
            android:layout_alignStart="@+id/txtDiscount">
    
            <RadioButton
                android:text="@string/radExteriorInterior"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/radExteriorOnly"
                android:layout_alignStart="@+id/radExteriorOnly"
                android:layout_marginTop="22dp"
                android:id="@+id/radExteriorInterior" />
    
    
            <RadioButton
            android:text="@string/radExteriorOnly"
            android:layout_width="178dp"
            android:layout_height="wrap_content"
            android:id="@+id/radExteriorOnly"
            android:layout_below="@+id/numOfWashes"
            android:layout_toEndOf="@+id/radioGroup4"
            android:layout_marginStart="13dp" />
        </RadioGroup>
    
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/numOfWashes"
            android:hint="@string/numOfWashes"
            android:layout_below="@+id/txtDiscount"
            android:layout_alignEnd="@+id/txtTitle"
            android:layout_alignStart="@+id/btnCalulate" />
    
    </RelativeLayout>
    

1 个答案:

答案 0 :(得分:0)

您对DecimalFormat的使用不正确;你基本上有:

DecimalFormat tenth = new DecimalFormat("#.#");
result.setText(tenth.format("$" + total + " for " + washesEntered + " washes"));

DecimalFormat仅用于格式化单个数字而不是整行输出,如下所示:

result.setText("$" + tenth.format(total) + " for " + washesEntered + " washes"));

应该产生更好的结果。

(另请注意我关于测试washesEntered 120.5-1会发生什么情况的评论。)