Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8894969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:38:54+00:00 2026-06-14T23:38:54+00:00

I was trying to make another sample from SwipeyTabsSampleActivity developed in android-playground project Here

  • 0

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;
        }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:38:55+00:00Added an answer on June 14, 2026 at 11:38 pm

    getTab() is an instance method of a non-static inner class of SwipeyTabsSampleActivity, 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 to mViewPager. The OnclickListener is an anonymous inner class of the inner class of SwipeyTabsSampleActivity, 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 mViewPager was a non-final local variable of the getTab() method, it wouldn’t compile either.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have another simple question about Node. I'm trying to make a simple http
I need some help in bison grammar construction. From my another question: I'm trying
I'm trying to make an address redirect to another one without actually changing the
I am trying to make a button to add another input box every time
I'm trying to make an image appear on top of another and still show
I am trying to make a numerical value, say 5000, quickly change to another
i'm trying to find joomla plugins or another components to make joomla an OpenID
Update: A sample project reproducing this bug can be found here at Microsoft Connect
After wasting two days with this question (and trying to make it work), I've
I admit, I'm new at this whole Android stuff. I am trying to make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.