使用try-catch更新整数值并使用toast显示它

时间:2016-08-22 12:58:44

标签: java android

我正在尝试通过从edittext获取值并将其转换为整数来更新intdelay,如下所示,我正在使用toast来检查intdelay的更新值。

public static int intdelay=1000;

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

    delayedttxt=(EditText)findViewById(R.id.delayedttxt);
    String delay=delayedttxt.getText().toString();       //this will get a string
    try{
        int intdelay = Integer.parseInt(delay);
   }catch(NumberFormatException ex){ // handle your exception
    }

    Button btn=(Button)findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            flashLight.switchFlash();
            Toast.makeText(MainActivity.this,"your integer is " + intdelay , Toast.LENGTH_LONG).show();
        }
    });

intdelay值未更新。 我怎么解决这个问题?

注意:我必须使用try-catch结构,否则应用程序会崩溃。

当我删除try-catch时 代码如下:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        delayedttxt=(EditText)findViewById(R.id.delayedttxt);
        String delay=delayedttxt.getText().toString();
        final int intdelay = Integer.parseInt(delay);

        Button btn=(Button)findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            flashLight.switchFlash();
            Toast.makeText(MainActivity.this,"your integer is " + intdelay ,Toast.LENGTH_LONG).show();
        }
    });

崩溃后的logcat如下:

08-22 16:13:05.866 17636-17636/de.nocnoc.clean.cleanlight I/RequestQueue: Repeating capture request set.
08-22 16:13:05.876 17636-17790/de.nocnoc.clean.cleanlight W/LegacyRequestMapper: convertRequestMetadata - control.awbRegions setting is not supported, ignoring value
08-22 16:13:05.876 17636-17790/de.nocnoc.clean.cleanlight W/LegacyRequestMapper: Only received metering rectangles with weight 0.
08-22 16:13:06.866 17636-17636/de.nocnoc.clean.cleanlight I/RequestQueue: Repeating capture request cancelled.
08-22 16:13:06.866 17636-17636/de.nocnoc.clean.cleanlight I/RequestQueue: Repeating capture request set.
08-22 16:13:06.894 17636-17790/de.nocnoc.clean.cleanlight W/LegacyRequestMapper: convertRequestMetadata - control.awbRegions setting is not supported, ignoring value
08-22 16:13:06.894 17636-17790/de.nocnoc.clean.cleanlight W/LegacyRequestMapper: Only received metering rectangles with weight 0.
08-22 16:19:51.367 19348-19348/de.nocnoc.clean.cleanlight W/System: ClassLoader referenced unknown path: /data/app/de.nocnoc.clean.cleanlight-2/lib/arm
08-22 16:19:51.404 19348-19348/de.nocnoc.clean.cleanlight D/AndroidRuntime: Shutting down VM
08-22 16:19:51.414 19348-19348/de.nocnoc.clean.cleanlight E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: de.nocnoc.clean.cleanlight, PID: 19348
                                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{de.nocnoc.clean.cleanlight/de.nocnoc.clean.flashlight.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
                                                                                at android.app.ActivityThread.access$900(ActivityThread.java:154)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:234)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5526)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                             Caused by: java.lang.NumberFormatException: Invalid int: ""
                                                                                at java.lang.Integer.invalidInt(Integer.java:138)
                                                                                at java.lang.Integer.parseInt(Integer.java:358)
                                                                                at java.lang.Integer.parseInt(Integer.java:334)
                                                                                at de.nocnoc.clean.flashlight.MainActivity.onCreate(MainActivity.java:32)
                                                                                at android.app.Activity.performCreate(Activity.java:6285)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) 
                                                                                at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:234) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5526) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
08-22 16:19:53.050 19348-19348/de.nocnoc.clean.cleanlight I/Process: Sending signal. PID: 19348 SIG: 9

3 个答案:

答案 0 :(得分:1)

您只在intdelay获得onCreate,您应该在每次显示吐司之前阅读编辑文本的值。换句话说,在onClickListener内。最初编辑文本中没有任何值,因此Integer.parseInt调用将throw NumberFormatException

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
         String delay = delayedttxt.getText().toString();
         int intdelay = 0;
         try{
             intdelay = Integer.parseInt(delay);
         } catch(NumberFormatException ex){
           e.printStackTrace();// Never swallow exception
         }
         flashLight.switchFlash();
         Toast.makeText("Initial Delay : " + intdelay , Toast.LENGTH_LONG).show();
    }
});

答案 1 :(得分:0)

这种情况正在发生,因为在edittext中设置任何值之前,延迟会被初始化。移动这一行

String delay=delayedttxt.getText().toString();   

在onclick方法中,像这样 -

 Button btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    String delay=delayedttxt.getText().toString();
        flashLight.switchFlash();
        Toast.makeText(MainActivity.this,"your integer is " + intdelay , Toast.LENGTH_LONG).show();
    }
});

然后再试一次

答案 2 :(得分:0)

你应该把

String delay=delayedttxt.getText().toString();       //this will get a string
try{
    int intdelay = Integer.parseInt(delay);
}catch(NumberFormatException ex){ // handle your exception
}

在onclicklistener中 它正在抛出NumberFormatException,因为你正在解析getText方法返回的空字符串 这样做之后不需要尝试捕获