2016-01-22 80 views
-1

我正在做一些计算,但无法解析一个字符串为int或甚至在float.I搜索解决方案,我读它的地方必须有一个空字符串,但我检查使用java.lang.NumberFormatException:无效int:“”:错误

log.v("Valuee",e1.getText().toString()); 

和打印我的EDITTEXT值证明字符串不是空.. 什么,我缺少什么?

这里是logcat的

了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.farrukh.bmi/com.example.farrukh.bmi.Main2Activity}:java.lang中。 NumberFormatException:无效的int:“” at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access $ 800( ActivityThread.java:135) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java :779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) 导致:java.lang.NumberFormatException:Invalid int:“ “ at java.lang.Integer.invalidInt(Integer.java:137) at java.lang.Integer.parseInt(Integer.java:358) at java.lang.Integer.parseInt(Integer.java:331) at com.example.farrukh.bmi.Main2Activity.onCreate(Main2Activity.java:31) at android.app.Activity.performCreate(Activity.java:5231) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 在android.app.ActivityThread.access $ 800(ActivityThread.java:135) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:136) at android.a pp.ActivityThread.main(ActivityThread.java:5017) 在java.lang.reflect.Method.invokeNative(本机方法)

这里是MainActivity.java

public class Main2Activity extends AppCompatActivity { 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main2); 

      final Button b = (Button) findViewById(R.id.button); 
      final EditText e1 = (EditText) findViewById(R.id.editText2); 
      final TextView text = (TextView) findViewById(R.id.textView4); 
      final String height = e1.getText().toString(); 


      final int a = Integer.parseInt(height); //got error while parsing 

    b.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 


       // Log.v("EditText",e1.getText().toString()); 

       } 
      }); 


     } 


    } 

这里是activity.xml

<Button 
     android:layout_width="132dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="50dp" 
     android:layout_gravity="center_horizontal" 
     android:text="CLICK" 
     android:clickable="true" 
     android:id="@+id/button"/> 

<EditText 
     android:layout_width="120dp" 
     android:layout_height="wrap_content" 
     android:inputType="number" 
     android:ems="10" 
     android:id="@+id/editText2" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="20dp" 
     android:gravity="center" /> 


    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:id="@+id/textView4" 
     android:layout_gravity="center_horizontal" 
     android:gravity="center" 
     android:layout_marginTop="20dp" 
     android:textColor="#3b9ff0" /> 
+2

因为在e1中没有价值。首先点击进行验证,然后转换为整数 – Pavya

+2

e1包含空白文本,因此需要使用gettext()内部onclick() –

+1

有问题,NumberFormatException不是空指针异常。 –

回答

2

将这些行内onClick()

final String height = e1.getText().toString(); 
final int a = Integer.parseInt(height); 

您在onCreate()取的e1值,当你想它时,按钮用户点击

还需要检查高度是否有任何值或不,检查Stuluske's回答这个问题

+0

感谢downvote,我可以知道原因吗? –

+0

可能是因为你没有考虑到这个高度可能不是一个数字。然而,我提高了这个答案,因为在'onCreate()'过程中使用'getText()'是没有用的。 – LordAnomander

+0

@LordAnomander哦,是的,我忘了那一点,将更新我的回答 –

2

有没有价值但当onCreate()运行。在您的点击监听器中移动getText()parseInt(),以便在输入内容时读取并解析该值。

8

您试图将空字符串(“”)解析为数值,但“”不是数字值。

确保将其设置为必需,或在尝试解析之前检查空白。

final int a = !height.equals("")?Integer.parseInt(height) : 0; 

例如。

编辑:

如果您已添加空格,那么它将是“”;

height = height.trim(); 
final int a = !height.equals("")?Integer.parseInt(height) : 0; 

应该做的伎俩。

+1

如果我添加了2个空格怎么办 – Pavya

+0

我在答案中添加了回复。 – Stultuske

+1

这样可以避免崩溃,但是,由于代码运行得太早,不允许OP实际读取输入的值。 – laalto

0

在做Integer.parseInt之前进行检查,如

final int a = (height == null || height.trim().equal("") ? 0 : Integer.parseInt(height)); 

你也需要添加该代码从的onclick监听器里的EditText得到高度值在OnCreate中控件时不会有任何除了你的值设置在layout.xml一些默认值是刚刚创建它

相关问题