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 8946073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:25:07+00:00 2026-06-15T12:25:07+00:00

I try to write an app, that is using slide effect for activities and

  • 0

I try to write an app, that is using slide effect for activities and found that ViewPager from Compatibility Package is what I need. I successfully made the slide, it works in the way i want, but my problem is that I am not able to refer to the elements in my Activity.

This is my main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

MainActivity.java:

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MyPagerAdapter adapter = new MyPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.pager);

        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);

        if (Fibonacci.getInstance().number != 0)
            displayResults();

        ImageButton btnCalculate = (ImageButton) findViewById(R.id.btnCalculateDark);
        btnCalculate.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                // do button click
            }
        });
    }

    private class MyPagerAdapter extends PagerAdapter {
        public int getCount() {
            return 2;
        }

        public Object instantiateItem(View collection, int position) {
            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            int resId = 0;
            switch (position) {
            case 0:
                resId = R.layout.activity_dark;
                break;
            case 1:
                resId = R.layout.activity_pale;
                break;
            }
            View view = inflater.inflate(resId, null);
            ((ViewPager) collection).addView(view, 0);
            return view;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView((View) arg2);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == ((View) arg1);
        }

        @Override
        public Parcelable saveState() {
            return null;
        }
    }
}

My problem is that I receive NullPointerEception for the following line:

ImageButton btnCalculate = (ImageButton) findViewById(R.id.btnCalculateDark);

I know it’s because in setContentView I set up main.xml, and I want to refer to an element in activity_dark.xml or activity_pale.xml. Unfortunately i am not able to figure out how to refer to the element. Any hints would be appreciated! Thank you!

  • 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-15T12:25:08+00:00Added an answer on June 15, 2026 at 12:25 pm

    You should better use Fragments for this
    http://developer.android.com/guide/components/fragments.html

    but if you want do it like this, then try to set listener after page changed:

     myPager.setOnPageChangeListener(new OnPageChangeListener(){
    
                @Override
                public void onPageScrollStateChanged(int arg0) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void onPageSelected(int arg0) {
                    ImageButton btnCalculate = (ImageButton) findViewById(R.id.btnCalculateDark);
                    btnCalculate.setOnClickListener(new OnClickListener() {
                        public void onClick(View arg0) {
                            // do button click
                        }
                    });
                }
    
            });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write an app that allows any number of web clients to
I am using the example app from android : BluetoothChat. But when I try
Im using the eclipse plugin to try and write an android app. I looked
I try to write a Facebook app. This app will communicate with a rfid
I try to write a javascript Self-Executing Anonymous Function window.App = window.App || {}
guys. I have a strange problem. I try to write unit-tests to web-app. I
try to write a composite component that allows mutltiple text inputs. I read that
I try to write Activator action for changing current song rating. I now that
I try to write a macro in clojure that sets up a namespace and
I have been trying to write an app that periodically parses the contents of

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.