-9

我的 android 应用程序仍然给我这个下面的错误。不幸的是,当我在模拟器中单击应用程序图标时,我的应用程序已停止工作。

以下是Logcat错误

01-04 11:20:57.440: E/AndroidRuntime(798): FATAL EXCEPTION: main
01-04 11:20:57.440: E/AndroidRuntime(798): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.oj2.currrencyconverter/com.oj2.currrencyconverter.MainConverter}: java.lang.NumberFormatException: Invalid int: ""
01-04 11:20:57.440: E/AndroidRuntime(798):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-04 11:20:57.440: E/AndroidRuntime(798):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-04 11:20:57.440: E/AndroidRuntime(798):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-04 11:20:57.440: E/AndroidRuntime(798):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-04 11:20:57.440: E/AndroidRuntime(798):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 11:20:57.440: E/AndroidRuntime(798):  at android.os.Looper.loop(Looper.java:137)
01-04 11:20:57.440: E/AndroidRuntime(798):  at android.app.ActivityThread.main(ActivityThread.java:5039)
01-04 11:20:57.440: E/AndroidRuntime(798):  at java.lang.reflect.Method.invokeNative(Native Method)
01-04 11:20:57.440: E/AndroidRuntime(798):  at java.lang.reflect.Method.invoke(Method.java:511)
01-04 11:20:57.440: E/AndroidRuntime(798):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-04 11:20:57.440: E/AndroidRuntime(798):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-04 11:20:57.440: E/AndroidRuntime(798):  at dalvik.system.NativeStart.main(Native Method)
01-04 11:20:57.440: E/AndroidRuntime(798): Caused by: java.lang.NumberFormatException: Invalid int: ""
01-04 11:20:57.440: E/AndroidRuntime(798):  at java.lang.Integer.invalidInt(Integer.java:138)
01-04 11:20:57.440: E/AndroidRuntime(798):  at java.lang.Integer.parseInt(Integer.java:359)
01-04 11:20:57.440: E/AndroidRuntime(798):  at java.lang.Integer.parseInt(Integer.java:332)
01-04 11:20:57.440: E/AndroidRuntime(798):  at com.oj2.currrencyconverter.MainConverter.onCreate(MainConverter.java:28)
01-04 11:20:57.440: E/AndroidRuntime(798):  at android.app.Activity.performCreate(Activity.java:5104)
01-04 11:20:57.440: E/AndroidRuntime(798):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-04 11:20:57.440: E/AndroidRuntime(798):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-04 11:20:57.440: E/AndroidRuntime(798):  ... 11 more

以下是代码

package com.oj2.currrencyconverter;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class MainConverter extends Activity {
    EditText dollars; 
    EditText rupees;
    RadioButton dTOr;
    RadioButton rTOd;
    Button convert;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //set Gui gui_main_converter for class MainConverter
        setContentView(R.layout.gui_main_converter);
        //Get the value from edittext in dollars
        dollars = (EditText)findViewById(R.id.dollaredtxt);
        //
        String rup = dollars.getText().toString();
        int convertrup = Integer.parseInt(rup);
        rupees = (EditText)findViewById(R.id.edtrup);
        dTOr = (RadioButton) findViewById(R.id.radioDtoR);
        dTOr.setChecked(true);
        rTOd = (RadioButton) findViewById(R.id.radioRtoD);
        convert = (Button) findViewById(R.id.btnConvert);
        convert.setOnClickListener(mylistner);
    }
    private OnClickListener mylistner = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (dTOr.isChecked()) {
                ConvertDollarToRup();
            }
            if (rTOd.isChecked()) {
                ConvertRupToDollar();
            }
        }
    };
    public void ConvertDollarToRup() {
        // TODO Auto-generated method stub
        double val = Double.parseDouble(dollars.getText().toString());
        rupees.setText(Double.toString(val*56));
    }
    public void ConvertRupToDollar() {
        // TODO Auto-generated method stub
        double val = Double.parseDouble(rupees.getText().toString());
        dollars.setText(Double.toString(val/56));

    }

}

感谢您

4

2 回答 2

0

您正在尝试将空字符串转换为整数:

dollars = (EditText)findViewById(R.id.dollaredtxt);
String rup = dollars.getText().toString();
int convertrup = Integer.parseInt(rup);

rup就是这样:(""空字符串)
如果您的 EditText 最初不包含值,则字符串将始终为空。

您必须在onClick方法中而不是onCreate方法中进行解析。

于 2013-03-07T16:40:04.233 回答
0

像这样改变你的方法:

public void ConvertDollarToRup() {
    if(!dollars.getText().toString().equals("")) {
        double val = Double.parseDouble(dollars.getText().toString());
        rupees.setText(Double.toString(val*56));
    }
}

public void ConvertRupToDollar() {
    if(!rupees.getText().toString().equals("")) {
        double val = Double.parseDouble(rupees.getText().toString());
        dollars.setText(Double.toString(val/56));
    }
}
于 2013-01-04T12:15:08.137 回答