+ 1

Why is this code throwing an error "local variable addition defined in enclosing scope should be final or effectievely final".

plus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {screen.setText(screen.getText()+"+"); addition=true; } }); I even added "final" before addition like the below code , but now it shows float expected and not final. plus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {screen.setText(screen.getText()+"+"); final addition=true; } });

12th Nov 2020, 8:25 AM
Mons Joseph
Mons Joseph - avatar
4 Respuestas
+ 7
//plus.setOnClickListener(v -> screen.setText(screen.getText() + "+")); plus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {screen.setText(screen.getText()+"+"); addition=true; } }); //addition if(screen != null){ equals.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { String[]x=screen.getText().toString().split("\\+"); screen.setText(String.valueOf(Integer.parseInt(x[0])+Integer.parseInt(x[1]))); } }); } } } Part-3
12th Nov 2020, 12:49 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 6
divide=(Button)findViewById(R.id.divide); cancel=(Button)findViewById(R.id.cancel);*/ plus=(Button)findViewById(R.id.plus); equals=(Button)findViewById(R.id.equals); screen=(EditText)findViewById(R.id.screen); //the hard way of showing clicks on screen class MyListener implements View.OnClickListener { @Override public void onClick(View v) { screen.setText(screen.getText() + "1"); } } MyListener listener = new MyListener(); one.setOnClickListener(listener); //simplified way of showing clicks on screen two.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {screen.setText(screen.getText() + "2");} }); //using lambda to simplify even more.. //You can now use some of the Java 8 features in Android development including lambda expression and method references.And you can use lamdas in your projects. //With lambda expression, Part-2
12th Nov 2020, 12:48 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 4
I think you missed the boolean type to declare, or you are doing it wrong (way). I think something like this would be genuine, but the problem is final variables can't be changed or modified. final boolean addition; plus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {screen.setText(screen.getText()+"+"); addition=true; } }); Or if you want an alternative to this , without using final than you need to declare your addition variable as class field (something similar to the global variable). i.g public class MainActivity extends Activity { //Static field public static boolean addition; @Override protected void onCreate(Bundle savedInstanceState) { //Other stuff here plus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ screen.setText(screen.getText()+"+"); addition=true; }}; }
12th Nov 2020, 9:35 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 4
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { Button one,plus,two,three,four,five,six,seven,eight,nine,zero,cancel,multiply,divide,minus,equals; EditText screen; boolean addition; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // one,two etc... one=(Button)findViewById(R.id.one); two=(Button)findViewById(R.id.two); /*three=(Button)findViewById(R.id.three); four=(Button)findViewById(R.id.four); five=(Button)findViewById(R.id.five); six=(Button)findViewById(R.id.six); seven=(Button)findViewById(R.id.seven); eight=(Button)findViewById(R.id.eight); nine=(Button)findViewById(R.id.nine); zero=(Button)findViewById(R.id.zero); minus=(Button)findViewById(R.id.minus); multiply=(Button)findViewById(R.id.multiply) Part-1
12th Nov 2020, 12:48 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar