I was trying to make another sample from SwipeyTabsSampleActivity developed in android-playground project
Here is the sample code and my question is why i did not get a compilation error upon referencing of two variables “mViewPager” & “position” in method getTab->view.setOnClickListener
Here is the full sample source code. I tried the sample code first and it compiled and running well which I don’t know how meanwhile when I was trying to customize the sample according to my needs. Everything went fine (compilation & running) except when I intended to set onClickListener and referencing these two variables, I got the following two compilation errors
1- Cannot make a static reference to the non-static field mViewPager
2- Cannot refer to a non-final variable position inside an inner class defined in a different method
I think that these two errors should have appear in the sample code as well not only my code but I dont know why?
/*
* Copyright 2011 Peter Kuterna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.peterkuterna.android.apps.swipeytabs;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class SwipeyTabsSampleActivity extends FragmentActivity {
private static final String [] TITLES = {
"CATEGORIES",
"FEATURED",
"TOP PAID",
"TOP FREE",
"TOP GROSSING",
"TOP NEW PAID",
"TOP NEW FREE",
"TRENDING",
};
private SwipeyTabs mTabs;
private ViewPager mViewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipeytab);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mTabs = (SwipeyTabs) findViewById(R.id.swipeytabs);
SwipeyTabsPagerAdapter adapter = new SwipeyTabsPagerAdapter(this,
getSupportFragmentManager());
mViewPager.setAdapter(adapter);
mTabs.setAdapter(adapter);
mViewPager.setOnPageChangeListener(mTabs);
mViewPager.setCurrentItem(0);
}
private class SwipeyTabsPagerAdapter extends FragmentPagerAdapter implements
SwipeyTabsAdapter {
private final Context mContext;
public SwipeyTabsPagerAdapter(Context context, FragmentManager fm) {
super(fm);
this.mContext = context;
}
@Override
public Fragment getItem(int position) {
return SwipeyTabFragment.newInstance(TITLES[position]);
}
@Override
public int getCount() {
return TITLES.length;
}
public TextView getTab(final int position, SwipeyTabs root) {
TextView view = (TextView) LayoutInflater.from(mContext).inflate(
R.layout.swipey_tab_indicator, root, false);
view.setText(TITLES[position]);
view.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mViewPager.setCurrentItem(position);
}
});
return view;
}
}
}
getTab()is an instance method of a non-static inner class ofSwipeyTabsSampleActivity, so it has an implicit reference to its outer class instance, and has access to all the fields and methods of this instance, and thus tomViewPager. TheOnclickListeneris an anonymous inner class of the inner class ofSwipeyTabsSampleActivity, and thus also has access to all the fields and methods of its two outer class instances.If the inner class was static, it wouldn’t compile. If
mViewPagerwas a non-final local variable of thegetTab()method, it wouldn’t compile either.