Android活动崩溃

时间:2012-05-22 11:52:28

标签: android

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

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>

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

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

我已经重启了日食,多次了。 清理了构建并删除了R.java文件几次,但我仍然被困在这里,不知道接下来该做什么。

4 个答案:

答案 0 :(得分:1)

OnCreate方法中,您正在计算字段并从TextView中检索双打。不幸的是,您的TextView是空白的,因此无法转换为double。

这是问题:

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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

好像你试图在这里使用一个字符串作为一个双...如果你确定该值将是一个双倍,也许你可以投两个

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

答案 3 :(得分:0)

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