Ans Ans - 3 months ago 20
Java Question

Updating an integer value using try-catch and showing it using a toast

I am trying to update

intdelay
by taking the value from an edittext and converting it into an integer as shown below, and I am using a toast to check the updated value of
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();
}
});


but the
intdelay
value is not getting updated.
How I can solve that?

Note: I have to use the try-catch structure because otherwise the app crashes.

When I remove the try-catch,
the code becomes as following:

@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();
}
});


and the logcat after crash as following:

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

Answer

You are getting intdelay only in onCreate, you should read the value of edit text every time before displaying the toast. In other way inside onClickListener. Initially the edit text will not have any value in it and so Integer.parseInt call will 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();
    }
});
Comments