1

我做了一个简单的汽车里程计算器。

package com.android.carmanager;

import com.android.carmanager.CarManagerActivity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;


public class MileageCalculator extends CarManagerActivity {
private EditText input1;
private EditText input2;
private EditText input3;
private TextView distance;
private TextView showmileage;


  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mileagecalculator);

        input1 = (EditText) findViewById(R.id.input1);
        input2 = (EditText) findViewById(R.id.input2);
        input3 = (EditText) findViewById(R.id.input3);
        distance = (TextView) findViewById(R.id.distance);
        showmileage = (TextView) findViewById(R.id.showmileage);

        Button calculatebutton = (Button) findViewById(R.id.calculatebutton);

        double totaldistance = new Double(input2.getText().toString()) - new Double(input1.getText().toString());
        distance.setText(Double.toString(totaldistance));

        calculatebutton.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {

                double result = new Double(distance.getText().toString()) / new Double(input3.getText().toString());
                showmileage.setText(Double.toString(result));

            }

    });


}
}

里程计算器 .xml 文件在这里:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/InitialOdo"
        android:padding="10dp" android:textStyle="bold"/>

    <EditText
        android:id="@+id/input1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:padding="10dp" >


    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/FinalOdo"
        android:padding="10dp" 
        android:textStyle="bold"/>

    <EditText
        android:id="@+id/input2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:padding="10dp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="@string/distance"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/distance"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"/>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/fuelrefilled"
        android:padding="10dp" android:textStyle="bold"/>

    <EditText
        android:id="@+id/input3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:padding="10dp" />

    <Button
        android:id="@+id/calculatebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/CalcMileage" />

    <TextView
        android:id="@+id/showmileage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|center_horizontal" />

</LinearLayout>

每当我尝试运行这个简单的里程计算器活动时,应用程序简单崩溃都会返回到主屏幕。我在日志猫中遇到以下错误。

05-22 17:05:09.743: W/dalvikvm(542): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-22 17:05:09.793: E/AndroidRuntime(542): FATAL EXCEPTION: main
05-22 17:05:09.793: E/AndroidRuntime(542): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.carmanager/com.android.carmanager.MileageCalculator}: java.lang.NumberFormatException: Invalid double: ""
05-22 17:05:09.793: E/AndroidRuntime(542):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-22 17:05:09.793: E/AndroidRuntime(542):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-22 17:05:09.793: E/AndroidRuntime(542):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-22 17:05:09.793: E/AndroidRuntime(542):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-22 17:05:09.793: E/AndroidRuntime(542):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 17:05:09.793: E/AndroidRuntime(542):  at android.os.Looper.loop(Looper.java:137)
05-22 17:05:09.793: E/AndroidRuntime(542):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-22 17:05:09.793: E/AndroidRuntime(542):  at java.lang.reflect.Method.invokeNative(Native Method)
05-22 17:05:09.793: E/AndroidRuntime(542):  at java.lang.reflect.Method.invoke(Method.java:511)
05-22 17:05:09.793: E/AndroidRuntime(542):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-22 17:05:09.793: E/AndroidRuntime(542):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-22 17:05:09.793: E/AndroidRuntime(542):  at dalvik.system.NativeStart.main(Native Method)
05-22 17:05:09.793: E/AndroidRuntime(542): Caused by: java.lang.NumberFormatException: Invalid double: ""
05-22 17:05:09.793: E/AndroidRuntime(542):  at java.lang.StringToReal.invalidReal(StringToReal.java:63)
05-22 17:05:09.793: E/AndroidRuntime(542):  at java.lang.StringToReal.parseDouble(StringToReal.java:248)
05-22 17:05:09.793: E/AndroidRuntime(542):  at java.lang.Double.parseDouble(Double.java:295)
05-22 17:05:09.793: E/AndroidRuntime(542):  at java.lang.Double.<init>(Double.java:136)
05-22 17:05:09.793: E/AndroidRuntime(542):  at com.android.carmanager.MileageCalculator.onCreate(MileageCalculator.java:30)
05-22 17:05:09.793: E/AndroidRuntime(542):  at android.app.Activity.performCreate(Activity.java:4465)
05-22 17:05:09.793: E/AndroidRuntime(542):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-22 17:05:09.793: E/AndroidRuntime(542):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-22 17:05:09.793: E/AndroidRuntime(542):  ... 11 more
05-22 17:05:10.033: I/dalvikvm(542): threadid=3: reacting to signal 3
05-22 17:05:10.043: I/dalvikvm(542): Wrote stack traces to '/data/anr/traces.txt'
05-22 17:05:10.493: I/dalvikvm(542): threadid=3: reacting to signal 3
05-22 17:05:10.563: I/dalvikvm(542): Wrote stack traces to '/data/anr/traces.txt'
05-22 17:05:12.313: I/Process(542): Sending signal. PID: 542 SIG: 9

我已经重新启动了eclipse,avd几次。多次清理构建并删除了 R.java 文件,但我仍然被困在这里,不知道下一步该做什么。

4

4 回答 4

1

在您的OnCreate方法中,您正在计算一个字段并从 TextViews 中检索双精度数。不幸的是,您的 TextViews 是空白的,因此无法转换为双精度。

这是问题行:

double totaldistance = new Double(input2.getText().toString()) 
              - new Double(input1.getText().toString());

您需要将计算移动到仅按下按钮,在尝试计算之前对数据进行一些验证,或者设置一些默认值。

于 2012-05-22T11:55:51.550 回答
0

这就是导致问题的原因: double totaldistance = new Double(input2.getText().toString()) - new Double(input1.getText().toString());

由于您的文本输入字段可能为空,因此无法将其转换为浮点数。

于 2012-05-22T11:55:13.973 回答
0

似乎您在这里尝试将字符串用作双精度...如果您确定该值将是双精度,那么您可以转换为双精度

double totaldistance = new Double(input2.getText().toString()) - new Double(input1.getText().toString());
distance.setText(Double.toString(totaldistance));
于 2012-05-22T11:57:51.037 回答
0
double totaldistance = new Double(input2.getText().toString()) - new Double(input1.getText().toString());
distance.setText(Double.toString(totaldistance));
于 2012-05-22T12:00:25.233 回答