android - 当我按下按钮时崩溃(当我不在edittext字段中输入数据时)

时间:2012-01-08 17:08:39

标签: android

我有一个应用程序,其中我有一个带有一些编辑文本字段的屏幕,当用户输入数据时,然后按下执行计算的按钮。 但如果我不输入数据而只按下按钮,它就会崩溃。

我认为当没有数据时,然后抛出一条消息,但我不知道该怎么做!

还有其他办法吗?

这是这部分。它调用mass_calcs函数进行计算。

public void onClick(View v) {
        switch (v.getId()){
        case R.id.mass_calcs:
            mass_func();
            break;
        }

这是logcat:

01-08 18:45:25.650: I/jdwp(723): received file descriptor 13 from ADB
01-08 18:45:25.752: W/System.err(723): Can't dispatch DDM chunk 46454154: no handler defined
01-08 18:45:25.752: W/System.err(723): Can't dispatch DDM chunk 4d505251: no handler defined
01-08 18:49:13.350: W/InputMethodManager(723): Ignoring onBind: cur seq=2, given seq=1
01-08 18:49:14.999: W/KeyCharacterMap(723): No keyboard for id 0
01-08 18:49:14.999: W/KeyCharacterMap(723): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
01-08 18:49:15.589: D/AndroidRuntime(723): Shutting down VM
01-08 18:49:15.589: W/dalvikvm(723): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
01-08 18:49:15.599: E/AndroidRuntime(723): Uncaught handler: thread main exiting due to uncaught exception
01-08 18:49:15.692: E/AndroidRuntime(723): java.lang.NumberFormatException: 
01-08 18:49:15.692: E/AndroidRuntime(723):  at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:258)
01-08 18:49:15.692: E/AndroidRuntime(723):  at java.lang.Double.parseDouble(Double.java:323)
01-08 18:49:15.692: E/AndroidRuntime(723):  at com.wordpress.androiddevgeo.Radiation.number_mass.mass_func(number_mass.java:52)
01-08 18:49:15.692: E/AndroidRuntime(723):  at com.wordpress.androiddevgeo.Radiation.number_mass.onClick(number_mass.java:45)
01-08 18:49:15.692: E/AndroidRuntime(723):  at android.view.View.performClick(View.java:2179)
01-08 18:49:15.692: E/AndroidRuntime(723):  at android.view.View.onTouchEvent(View.java:3828)
01-08 18:49:15.692: E/AndroidRuntime(723):  at android.widget.TextView.onTouchEvent(TextView.java:6291)
01-08 18:49:15.692: E/AndroidRuntime(723):  at android.view.View.dispatchTouchEvent(View.java:3368)
01-08 18:49:15.692: E/AndroidRuntime(723):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
01-08 18:49:15.692: E/AndroidRuntime(723):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
01-08 18:49:15.692: E/AndroidRuntime(723):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
01-08 18:49:15.692: E/AndroidRuntime(723):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
01-08 18:49:15.692: E/AndroidRuntime(723):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1707)
01-08 18:49:15.692: E/AndroidRuntime(723):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1197)
01-08 18:49:15.692: E/AndroidRuntime(723):  at android.app.Activity.dispatchTouchEvent(Activity.java:1993)
01-08 18:49:15.692: E/AndroidRuntime(723):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1691)
01-08 18:49:15.692: E/AndroidRuntime(723):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1525)
01-08 18:49:15.692: E/AndroidRuntime(723):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 18:49:15.692: E/AndroidRuntime(723):  at android.os.Looper.loop(Looper.java:123)
01-08 18:49:15.692: E/AndroidRuntime(723):  at android.app.ActivityThread.main(ActivityThread.java:3948)
01-08 18:49:15.692: E/AndroidRuntime(723):  at java.lang.reflect.Method.invokeNative(Native Method)
01-08 18:49:15.692: E/AndroidRuntime(723):  at java.lang.reflect.Method.invoke(Method.java:521)
01-08 18:49:15.692: E/AndroidRuntime(723):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
01-08 18:49:15.692: E/AndroidRuntime(723):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
01-08 18:49:15.692: E/AndroidRuntime(723):  at dalvik.system.NativeStart.main(Native Method)
01-08 18:49:15.799: I/dalvikvm(723): threadid=7: reacting to signal 3
01-08 18:49:16.030: I/dalvikvm(723): Wrote stack trace to '/data/anr/traces.txt'
01-08 18:49:19.361: I/Process(723): Sending signal. PID: 723 SIG: 9
01-08 19:15:31.900: D/AndroidRuntime(767): Shutting down VM
01-08 19:15:31.900: W/dalvikvm(767): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
01-08 19:15:31.900: E/AndroidRuntime(767): Uncaught handler: thread main exiting due to uncaught exception
01-08 19:15:31.954: E/AndroidRuntime(767): java.lang.NumberFormatException: 
01-08 19:15:31.954: E/AndroidRuntime(767):  at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:258)
01-08 19:15:31.954: E/AndroidRuntime(767):  at java.lang.Double.parseDouble(Double.java:323)
01-08 19:15:31.954: E/AndroidRuntime(767):  at com.wordpress.androiddevgeo.Radiation.number_mass.mass_func(number_mass.java:51)
01-08 19:15:31.954: E/AndroidRuntime(767):  at com.wordpress.androiddevgeo.Radiation.number_mass.onClick(number_mass.java:45)
01-08 19:15:31.954: E/AndroidRuntime(767):  at android.view.View.performClick(View.java:2179)
01-08 19:15:31.954: E/AndroidRuntime(767):  at android.view.View.onTouchEvent(View.java:3828)
01-08 19:15:31.954: E/AndroidRuntime(767):  at android.widget.TextView.onTouchEvent(TextView.java:6291)
01-08 19:15:31.954: E/AndroidRuntime(767):  at android.view.View.dispatchTouchEvent(View.java:3368)
01-08 19:15:31.954: E/AndroidRuntime(767):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
01-08 19:15:31.954: E/AndroidRuntime(767):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
01-08 19:15:31.954: E/AndroidRuntime(767):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
01-08 19:15:31.954: E/AndroidRuntime(767):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
01-08 19:15:31.954: E/AndroidRuntime(767):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1707)
01-08 19:15:31.954: E/AndroidRuntime(767):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1197)
01-08 19:15:31.954: E/AndroidRuntime(767):  at android.app.Activity.dispatchTouchEvent(Activity.java:1993)
01-08 19:15:31.954: E/AndroidRuntime(767):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1691)
01-08 19:15:31.954: E/AndroidRuntime(767):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1525)
01-08 19:15:31.954: E/AndroidRuntime(767):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 19:15:31.954: E/AndroidRuntime(767):  at android.os.Looper.loop(Looper.java:123)
01-08 19:15:31.954: E/AndroidRuntime(767):  at android.app.ActivityThread.main(ActivityThread.java:3948)
01-08 19:15:31.954: E/AndroidRuntime(767):  at java.lang.reflect.Method.invokeNative(Native Method)
01-08 19:15:31.954: E/AndroidRuntime(767):  at java.lang.reflect.Method.invoke(Method.java:521)
01-08 19:15:31.954: E/AndroidRuntime(767):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
01-08 19:15:31.954: E/AndroidRuntime(767):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
01-08 19:15:31.954: E/AndroidRuntime(767):  at dalvik.system.NativeStart.main(Native Method)
01-08 19:15:32.021: I/dalvikvm(767): threadid=7: reacting to signal 3
01-08 19:15:32.259: I/dalvikvm(767): Wrote stack trace to '/data/anr/traces.txt'
01-08 19:15:34.082: I/Process(767): Sending signal. PID: 767 SIG: 9
01-08 19:16:41.569: W/KeyCharacterMap(774): No keyboard for id 0
01-08 19:16:41.582: W/KeyCharacterMap(774): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
01-08 19:16:45.840: D/AndroidRuntime(774): Shutting down VM
01-08 19:16:45.840: W/dalvikvm(774): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
01-08 19:16:45.840: E/AndroidRuntime(774): Uncaught handler: thread main exiting due to uncaught exception
01-08 19:16:45.911: E/AndroidRuntime(774): java.lang.NumberFormatException: 
01-08 19:16:45.911: E/AndroidRuntime(774):  at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:258)
01-08 19:16:45.911: E/AndroidRuntime(774):  at java.lang.Double.parseDouble(Double.java:323)
01-08 19:16:45.911: E/AndroidRuntime(774):  at com.wordpress.androiddevgeo.Radiation.number_mass.mass_func(number_mass.java:51)
01-08 19:16:45.911: E/AndroidRuntime(774):  at com.wordpress.androiddevgeo.Radiation.number_mass.onClick(number_mass.java:45)
01-08 19:16:45.911: E/AndroidRuntime(774):  at android.view.View.performClick(View.java:2179)
01-08 19:16:45.911: E/AndroidRuntime(774):  at android.view.View.onTouchEvent(View.java:3828)
01-08 19:16:45.911: E/AndroidRuntime(774):  at android.widget.TextView.onTouchEvent(TextView.java:6291)
01-08 19:16:45.911: E/AndroidRuntime(774):  at android.view.View.dispatchTouchEvent(View.java:3368)
01-08 19:16:45.911: E/AndroidRuntime(774):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
01-08 19:16:45.911: E/AndroidRuntime(774):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
01-08 19:16:45.911: E/AndroidRuntime(774):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
01-08 19:16:45.911: E/AndroidRuntime(774):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
01-08 19:16:45.911: E/AndroidRuntime(774):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1707)
01-08 19:16:45.911: E/AndroidRuntime(774):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1197)
01-08 19:16:45.911: E/AndroidRuntime(774):  at android.app.Activity.dispatchTouchEvent(Activity.java:1993)
01-08 19:16:45.911: E/AndroidRuntime(774):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1691)
01-08 19:16:45.911: E/AndroidRuntime(774):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1525)
01-08 19:16:45.911: E/AndroidRuntime(774):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 19:16:45.911: E/AndroidRuntime(774):  at android.os.Looper.loop(Looper.java:123)
01-08 19:16:45.911: E/AndroidRuntime(774):  at android.app.ActivityThread.main(ActivityThread.java:3948)
01-08 19:16:45.911: E/AndroidRuntime(774):  at java.lang.reflect.Method.invokeNative(Native Method)
01-08 19:16:45.911: E/AndroidRuntime(774):  at java.lang.reflect.Method.invoke(Method.java:521)
01-08 19:16:45.911: E/AndroidRuntime(774):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
01-08 19:16:45.911: E/AndroidRuntime(774):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
01-08 19:16:45.911: E/AndroidRuntime(774):  at dalvik.system.NativeStart.main(Native Method)
01-08 19:16:45.960: I/dalvikvm(774): threadid=7: reacting to signal 3
01-08 19:16:46.141: I/dalvikvm(774): Wrote stack trace to '/data/anr/traces.txt'
01-08 19:16:47.409: I/Process(774): Sending signal. PID: 774 SIG: 9

mass_calcs类是:

public class mass_calcs extends Activity  {

    TextView masscalcs;


     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.mass_calcs);

        masscalcs=(TextView) findViewById(R.id.masscalcs);       
        double res=getIntent().getExtras().getDouble("value");

        masscalcs.setText("Amount of mass remaining: "+res+" mg");
    }



}

3 个答案:

答案 0 :(得分:2)

在getDouble()上没有数据,当没有数据时,您会发现应用程序崩溃,因为没有提供双重功能。

你在哪里

case R.id.mass_calcs:
        mass_func();
        break;
    }

更改它以便

case R.id.mass_calcs:

        if(isNumeric(masscalcs.getText().toString()))
        {
            mass_func();
        }
        else
        {
            Toast.makeText(context, "Please provide a number", Toast.LENGTH_SHORT).show();
        }
        break;
    }

    public static boolean isNumeric(String str)  
    {     
      try  
      {  
        double d = Double.parseDouble(str);  
      }  
      catch(NumberFormatException nfe)  
      {  
        return false;  
      }  
      return true;  
    }

答案 1 :(得分:0)

你正在打电话

getDouble()

,但是当没有正确的数字时,它会崩溃。确保仅在输入正确的数字时调用它。

答案 2 :(得分:0)

您的EditText字符串是否可编辑?

  1. 如果它是可编辑的,我认为你不能使用
    Double.parseDouble();要在将意图存储在意图中时进行更改,请存储应存储字符串的editable.toString();
  2. 如果你已经传入了一个字符串,并且parseDouble()参数中没有任何内容(如果你传递的是一个空字符串),它也会抛出一个异常。为避免这种情况,您应该在将EditText字符串存储到intent中的区域中使用if-else,以检查字符串是否为null或具有某些内容。