简体   繁体   中英

Android Studio variable final declaration problem

I have a annoying problem. These codes were working 2 days ago but now it says that "takim1Text" have to be declared final and might not have been initialized . 2 days ago I dont need to declare it final. what is matter now? There codes... thank you by the way

Button devamButton;
    EditText takim1, takim2;
    devamButton = (Button) findViewById(R.id.devamButton);
    takim1 = (EditText) findViewById(R.id.takimA);
    takim2 = (EditText) findViewById(R.id.takimB);
    String takim1Text;
    String takim2Text;
    devamButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            takim1Text = takim1.getText().toString();
            takim2Text = takim2.getText().toString();
            if (takim2Text.equals(takim1Text)) {
                Toast toast1 = Toast.makeText(getApplicationContext(), "cant be same", Toast.LENGTH_SHORT);
                toast1.show();
            } else if (takim1Text.isEmpty() || takim2Text.isEmpty()) {
                Toast toast2 = Toast.makeText(getApplicationContext(), "cant be empty", Toast.LENGTH_SHORT);
                toast2.show();
            } else {
                Toast toast3 = Toast.makeText(getApplicationContext(), "works", Toast.LENGTH_SHORT);
                toast3.show();
            }
        }
    });

You should do the declaration global like this :

public class MainActivity extends AppCompatActivity {

protected Button btn;


String name;

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

I solved problem. just made variables global

Button devamButton;
EditText takim1, takim2;
String takimTextA;
String takimTextB;
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    devamButton = findViewById(R.id.devamButton);
    takim1 = findViewById(R.id.takimA);
    takim2 = findViewById(R.id.takimB);
    devamButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            takimTextA = takim1.getText().toString();
            takimTextB = takim2.getText().toString();
            if (takimTextA.equals(takimTextB)) {
                Toast toast1 = Toast.makeText(getApplicationContext(), "cant be same", Toast.LENGTH_SHORT);
                toast1.show();
            } else if (takimTextA.isEmpty() || takimTextB.isEmpty()) {
                Toast toast2 = Toast.makeText(getApplicationContext(), "cant be empty", Toast.LENGTH_SHORT);
                toast2.show();
            } else {
                Toast toast3 = Toast.makeText(getApplicationContext(), "works", Toast.LENGTH_SHORT);
                toast3.show();
            }
        }
    });

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