編譯完美,但在午餐時崩潰... LogCat說:「無法啓動活動ComponentInfo ........ 這裏是Main.xml,Activity的代碼。 Java和logcat的......簡單的單位轉換器,但在午餐時崩潰
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left|right"
android:orientation="vertical"
android:background="#66FFFF" >
<FrameLayout
android:id="@+id/frameLayout2"
android:layout_width="match_parent"
android:layout_height="20dp" >
</FrameLayout>
<TextView
android:id="@+id/UnitText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/UnitText"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="@+id/Spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/UnitPrompt"/>
<FrameLayout
android:id="@+id/frameLayout3"
android:layout_width="match_parent"
android:layout_height="20dp" >
</FrameLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="vertical" >
<Button
android:onClick="converter"
android:id="@+id/ConvertButton"
android:layout_width="131dp"
android:layout_height="wrap_content"
android:text="@string/ConvertText" />
</LinearLayout>
<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="match_parent"
android:layout_height="30dip" >
</FrameLayout>
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/BeforeBox"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/BeforeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/BeforeText"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/AfterBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/AfterText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/AfterText"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
Activity.Java
package arirang.unit.converter;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.EditText;
import android.widget.TextView;
public class BasicUnitConverterActivity extends Activity {
EditText val3;
String val2;
int val1;
String forResult;
Spinner Type;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner1 = (Spinner) findViewById(R.id.Spinner1);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
this, R.array.UnitList, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
EditText val3 = (EditText)findViewById(R.id.BeforeBox);
String val2 = val3.getText().toString();
val1 = Integer.parseInt(val2);
forResult = changeFormat(converter(val1));
TextView AfterBox = (TextView)findViewById(R.id.AfterBox);
AfterBox.setText(forResult);
}
private String changeFormat(Double dbResult) {
String result;
DecimalFormat format = new DecimalFormat("##.##");
result = format.format(dbResult);
return result;
}
public double converter(double val1) {
Type = (Spinner)findViewById(R.id.Spinner1);
double dbResult = 0;
if(Type.getSelectedItem().toString().equals("meter to inch"))
{
dbResult = val1 * 39.3700787 ;
}
else if(Type.getSelectedItem().toString().equals("meter to feet"))
{
dbResult = val1 * 3.2808399;
}
else if(Type.getSelectedItem().toString().equals("feet to inch"))
{
dbResult = val1 * 12;
}
else if(Type.getSelectedItem().toString().equals("feet to meter"))
{
dbResult = val1/3.2808399;
}
else if(Type.getSelectedItem().toString().equals("inch to meter"))
{
dbResult = val1/39.3700787 ;
}
else if(Type.getSelectedItem().toString().equals("inch to feet"))
{
dbResult = val1/12;
}
else if(Type.getSelectedItem().toString().equals("kilogram to pound"))
{
dbResult = val1 * 2.20462262;
}
else if(Type.getSelectedItem().toString().equals("pound to kilogram"))
{
dbResult = val1/2.20462262;
}
else if(Type.getSelectedItem().toString().equals("Fahrenheit to Celsius"))
{
dbResult=(val1 - 32)/(5/9);
}
else if(Type.getSelectedItem().toString().equals("Celsius to Fahrenheit"))
{
dbResult = (9/5) * val1 + 32;
}
return dbResult;
}
}
這是logcat的日誌
Logcat log
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x40015560)
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{arirang.unit.converter/arirang.unit.converter.BasicUnitConverterActivity}: java.lang.NumberFormatException: unable to parse '' as integer
E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
E/AndroidRuntime(330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime(330): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime(330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(330): Caused by: java.lang.NumberFormatException: unable to parse '' as integer
E/AndroidRuntime(330): at java.lang.Integer.parseInt(Integer.java:362)
E/AndroidRuntime(330): at java.lang.Integer.parseInt(Integer.java:332)
E/AndroidRuntime(330): at arirang.unit.converter.BasicUnitConverterActivity.onCreate(BasicUnitConverterActivity.java:37)
E/AndroidRuntime(330): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
E/AndroidRuntime(330): ... 11 more
說真的,非常感謝你幫助我,即使這些代碼不短... ^^