public course fieldsActivity extends task {Button addSiteButton;Button cancelButton;Button signInButton;/** * dubbed when the activity is an initial created. */
Overridepublic void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); // to develop a tradition title bar because that activity home window requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.fields); // use custom layout title bar getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.topbar); Pager adapter = brand-new Pager(); ViewPager mPager = (ViewPager) findViewById(R.id.fieldspager); mPager.setAdapter(adapter); mPager.setCurrentItem(1); addSiteButton = (Button) findViewById(R.id.addSiteButton); addSiteButton.setOnClickListener(new View.OnClickListener()
Override public void onClick(View v) mPager.setCurrentItem(2, true); //Compilation error wake up here. ); cancelButton = (Button) findViewById(R.id.cancel_button); signInButton = (Button) findViewById(R.id.sign_in_button);

*

*

*

You have the right to declare the change final, or make it an circumstances (or global) variable. If you declare it final, friend won"t be able to readjust it later.

You are watching: Variable is accessed from within inner class

Any variable defined in a an approach and accessed by an anonymous inner class must it is in final. Otherwise, you could use the variable in the inner class, unaware that if the variable alters in the inside class, and also then the is used later in the enclosing scope, the alters made in the inner class did not persist in the enclosing scope. Basically, what wake up in the inner course stays in the within class.

I wrote a more in-depth explanation here. It additionally explains why circumstances and global variables carry out not need to be claimed final.


*

The error states it all, change:

ViewPager mPager = (ViewPager) findViewById(R.id.fieldspager);to

final ViewPager mPager = (ViewPager) findViewById(R.id.fieldspager);

*

Here"s a funny answer.

You can declare a final one-element range and adjust the aspects of the array all you want apparently. I"m certain it division the an extremely reason why this compiler preeminence was imposed in the first place however it"s handy as soon as you"re in a time-bind together I was today.

I actually can"t claim credit for this one. It to be IntelliJ"s recommendation! feel a little bit hacky. But doesn"t it seems ~ as poor as a worldwide variable so I believed it worth pointing out here. It"s simply one equipment to the problem. No necessarily the finest one.

final int<> tapCount = 0;addSiteButton.setOnClickListener(new View.OnClickListener() {
Veger said, you can make it last so that the variable deserve to be provided in the within class.

final ViewPager pager = (ViewPager) findViewById(R.id.fieldspager);I dubbed it pager fairly than mPager because you room using it together a neighborhood variable in the onCreate method. The m prefix is cusomarily booked for class member variables (i.e. Variables that are asserted at the start of the class and are obtainable to all class methods).

See more: How Tall Is Sabre Norris 2021

If you actually do need a class member variable, the doesn"t occupational to make it final because you can"t use findViewById to set its value until onCreate. The equipment is to not usage an cotton inner class. This way the mPager change doesn"t should be claimed final and can be supplied throughout the class.

public class MainActivity extends AppCompatActivity { private ViewPager mPager; private button mButton;
Override publicly void onCreate(Bundle savedInstanceState) // ... MPager = (ViewPager) findViewById(R.id.fieldspager); // ... MButton.setOnClickListener(myButtonClickHandler); View.OnClickListener myButtonClickHandler = brand-new View.OnClickListener() {