我正在尝试通过从Edittext中获取值并将其转换为整数(如下所示)来更新intdelay,并且我正在使用烤面包来检查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

最佳答案

您仅在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();
    }
});

09-25 18:51