简单的单位转换器,但在启动时崩溃

时间:2012-02-08 02:23:51

标签: android

编译完美,但在发布时崩溃...... 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 Logs

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

2 个答案:

答案 0 :(得分:3)

问题是当应用程序加载onCreate()被调用并且文本框中没有任何内容时所以当你调用val3.getText()。toString()时,什么都没有。我建议

  val3.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {
      val2 = val3.getText().toString()
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
});

因此,当您输入值时,它将在您输入后抓取它。

答案 1 :(得分:1)

看起来您在创建活动时立即执行转换,而不是在有人点击转换按钮时执行转换。您在布局中定义了onClick侦听器,但您没有使用它...尝试添加此功能:

public void converter(View view) {
    EditText val3 = (EditText)findViewById(R.id.BeforeBox);
    String val2 = val3.getText().toString();
    if(val2.length() > 0) {
        val1 = Integer.parseInt(val2);


        forResult = changeFormat(converter(val1));

        TextView AfterBox = (TextView)findViewById(R.id.AfterBox);
        AfterBox.setText(forResult);
    }
}

并将onCreate()更改为:

@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);
}