简体   繁体   中英

Local variable is accessed from within inner class

I have check out answers like thees Variable is accessed within inner class. Needs to be declared final but it does not address what I am after

I have an on click listener and here is the code

mUsername is a EditText that is already defined same with the button

mLoginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String username = mUsername.getText().toString();
            username = username.trim();
            ParseUser.logInInBackground(username, password, new LogInCallback() {
                    @Override
                    public void done(ParseUser parseUser, ParseException e) {
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            intent.putExtra("username", username);// get compile error here 
                            startActivity(intent);
                   }
          }
});

The error says to set String username = mUsername.getText().toString(); to final but then I can't redefine username with the trim? I don't know why it can be used in the logInInBackround as a parameter but not in the method?

Thanks for the help in advance.

You could trim it directly:

String username = mUsername.getText().toString().trim();

This saves you the intermediate local variable store.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM