簡単な車の走行距離計算機を作りました。
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));
}
});
}
}
また、mileagecalculator .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
日食、avdを数回再起動しました。ビルドをクリーンアップし、R.java ファイルを数回削除しましたが、それでもここで立ち往生しており、次に何をすべきかわかりません。