简体   繁体   English

从内部类内部访问局部变量

[英]Local variable is accessed from within inner class

I have check out answers like thees Variable is accessed within inner class. 我已经检查了答案,例如thees 在内部类中访问变量。 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 mUsername是一个EditText,已与按钮定义相同

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(); 该错误提示设置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? 我不知道为什么它可以在logInInBackround中用作参数,但不能在方法中使用?

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. 这样可以为您节省中间局部变量存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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