Android应用程序可以运行,但过了一段时间它崩溃了

时间:2012-10-18 04:52:26

标签: android

此应用程序运行良好,直到30秒内输入数字,它只是崩溃。我认为这是一个记忆问题,但我可能是错的。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:ems="10"
    android:inputType="numberSigned|numberDecimal" 
    android:imeOptions="actionDone">

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/editText1"
    android:ems="10" 
    android:inputType="numberSigned|numberDecimal"
    android:imeOptions="actionDone"/>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/editText1"
    android:layout_alignParentLeft="true"
    android:text="@string/C"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/editText2"
    android:layout_alignParentLeft="true"
    android:text="@string/F"
    android:textAppearance="?android:attr/textAppearanceLarge" />

public class MainActivity extends Activity {

EditText text1, text2, text3, text4, text5;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text1 = (EditText) findViewById(R.id.editText1);
    text1.addTextChangedListener(watcher1);
    text2 = (EditText) findViewById(R.id.editText2);
    text2.addTextChangedListener(watcher2);

    }
    // Celcius
    TextWatcher watcher1 = new TextWatcher() {
         public void afterTextChanged(Editable s) {
             if(text1.getText().toString().length() == 0)
             {
                 return;
             }
                if (text1.hasFocus() == true ) {
                    float inputValue = Float.parseFloat(text1.getText().toString());
                    text2.setText(String.valueOf(convertCelsiusToFahrenheit(inputValue)));
                }
            }
            public void beforeTextChanged(CharSequence s, int start, int count,
                            int after) {
            }
            public void onTextChanged(CharSequence s, int start, int before,
                            int count) {
            }
        };
    // Fahrenheit   
    TextWatcher watcher2 = new TextWatcher() {
        public void afterTextChanged(Editable s) {
            if(text2.getText().toString().length() == 0)
            {
                return;
            }
                if (text2.hasFocus() == true ) {
                    float inputValue = Float.parseFloat(text2.getText().toString());
                    text1.setText(String.valueOf(convertFahrenheitToCelsius(inputValue)));
            }
        }
        public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
        }
        public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
        }
    };



//Converts C to F
private float convertCelsiusToFahrenheit(float celsius) {
 return (float) ((celsius * 1.8) + 32);
}


//Converts F to C
private float convertFahrenheitToCelsius(float fahrenheit) {
 return (float) ((fahrenheit - 32) / 1.8);
    }

}

    10-18 13:57:18.201: D/AndroidRuntime(1280): Shutting down VM
10-18 13:57:18.201: W/dalvikvm(1280): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
10-18 13:57:18.291: E/AndroidRuntime(1280): FATAL EXCEPTION: main
10-18 13:57:18.291: E/AndroidRuntime(1280): java.lang.NumberFormatException: Invalid float: "-"
10-18 13:57:18.291: E/AndroidRuntime(1280):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at java.lang.StringToReal.initialParse(StringToReal.java:151)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at java.lang.StringToReal.parseFloat(StringToReal.java:304)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at java.lang.Float.parseFloat(Float.java:300)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at com.thenegative.temerature.converter2.MainActivity$1.afterTextChanged(MainActivity.java:31)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.widget.TextView.sendAfterTextChanged(TextView.java:7095)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8743)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:212)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:30)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.text.method.BaseKeyListener.backspaceOrForwardDelete(BaseKeyListener.java:94)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:49)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:155)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:138)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.widget.TextView.doKeyDown(TextView.java:5240)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.widget.TextView.onKeyDown(TextView.java:5059)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.view.View.dispatchKeyEvent(View.java:7086)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1892)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1369)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.app.Activity.dispatchKeyEvent(Activity.java:2356)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1819)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3575)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.view.ViewRootImpl.deliverKeyEvent(ViewRootImpl.java:3531)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3113)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2930)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.os.Looper.loop(Looper.java:137)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at java.lang.reflect.Method.invokeNative(Native Method)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at java.lang.reflect.Method.invoke(Method.java:511)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-18 13:57:18.291: E/AndroidRuntime(1280):     at dalvik.system.NativeStart.main(Native Method)
10-18 13:57:18.462: D/dalvikvm(1280): GC_CONCURRENT freed 275K, 4% free 8188K/8519K, paused 46ms+5ms, total 214ms

我认为这是一个内存问题,因为我在edittext中输入数字大约30秒-40秒后就崩溃了,在logcat中它一直说“GC_CONCURRENT释放434K,7%免费8167K / 8711K,暂停16ms + 16ms,总计173毫秒“,看起来也很慢。我也认为这可能只是一个模拟器问题。

EDIT1: 问题只发生在我删除时。

4 个答案:

答案 0 :(得分:1)

您可以使用try / catch来处理NumberFormatException并在异常情况下显示Toast。

答案 1 :(得分:0)

尝试将支票更新为:

if(text1.getText().toString().length() == 0)
{
    return;
}

答案 2 :(得分:0)

使用条件: -

if(!text1.getText().toString().equalIngorecase(""))
{
       float inputValue = Float.parseFloat(text1.getText().toString());
       text2.setText(String.valueOf(convertFahrenheitToCelsius(inputValue)));
}

答案 3 :(得分:0)

您无法将空字符串""解析为Float。 将您的代码(第50行)放入try块并捕获java.lang.NumberFormatException

更多信息here