尝试抓住按钮android

时间:2016-03-18 20:33:33

标签: java android button try-catch

我在edit123 = "";

时尝试构建try catch异常
public void OnClick(View v) {

    int a, d, n;
    String text = "";
    a = Integer.valueOf(edit1.getText().toString());
    d = Integer.valueOf(edit2.getText().toString());
    n = Integer.valueOf(edit3.getText().toString());

    try {
        for (int i = 1; i <= n; i++) {
            textView.setText(text += String.valueOf(a + (i - 1) * d + " "));
        }
    } catch (IllegalStateException e) {
        Toast toast = Toast.makeText(getApplicationContext(),
                "Не заданы данные!", Toast.LENGTH_SHORT);
        toast.show();
    }

还有编译器:

FATAL EXCEPTION: main
  java.lang.IllegalStateException: Could not execute method for android:onClick
  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
  at android.view.View.performClick(View.java:4204)
  at android.view.View$PerformClick.run(View.java:17355)
  at android.os.Handler.handleCallback(Handler.java:725)
  at android.os.Handler.dispatchMessage(Handler.java:92)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:5041)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
  at android.view.View.performClick(View.java:4204) 
  at android.view.View$PerformClick.run(View.java:17355) 
  at android.os.Handler.handleCallback(Handler.java:725) 
  at android.os.Handler.dispatchMessage(Handler.java:92) 
  at android.os.Looper.loop(Looper.java:137) 
  at android.app.ActivityThread.main(ActivityThread.java:5041) 
  at java.lang.reflect.Method.invokeNative(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:511) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
  at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NumberFormatException: Invalid int: ""
  at java.lang.Integer.invalidInt(Integer.java:138)
  at java.lang.Integer.parseInt(Integer.java:359)
  at java.lang.Integer.parseInt(Integer.java:332)
  at java.lang.Integer.valueOf(Integer.java:490)
  at cmplxt.p_4.MainActivity.OnClick(MainActivity.java:32)
  at java.lang.reflect.Method.invokeNative(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:511) 
  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
  at android.view.View.performClick(View.java:4204) 
  at android.view.View$PerformClick.run(View.java:17355) 
  at android.os.Handler.handleCallback(Handler.java:725) 
  at android.os.Handler.dispatchMessage(Handler.java:92) 
  at android.os.Looper.loop(Looper.java:137) 
  at android.app.ActivityThread.main(ActivityThread.java:5041) 
  at java.lang.reflect.Method.invokeNative(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:511) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
  at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:0)

您收到的是java.lang.NumberFormatException:无效的int:&#34;&#34;。您无法将空String转换为Integer。你应该添加&#34; java.lang.NumberFormatException&#34;在catch块中或在尝试转换之前检查String是否为空。