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

  • Home
  • SEARCH
  • 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 9145453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:31:06+00:00 2026-06-17T10:31:06+00:00

I’ve been working on this for a week now and I just can’t figure

  • 0

I’ve been working on this for a week now and I just can’t figure it out. I have a Pager Adapter with 4 views. It lets me swipe between them. But when I call onClickListener the app crashes on boot because the Listener is null. I’m a new programmer and I don’t understand how I can fix this problem. Please help! I’ve isolated the source of the problem, the view is not created or being destroyed after the Listener looks for the button ID and that’s why it’s null and crashing.

The method destroyItem is what I think is causing it, because when I remove it the app doesn’t crash until I start swiping threw views.

public class SplashPager extends PagerAdapter {

Button b;

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 4;
}

@Override
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.splash2;
        b = (Button) collection.findViewById(R.id.splashB);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                                    //setContentView(r.layout.someLayout);
            }
        });
        break;
    case 1:
        resId = R.layout.splash3;
        break;
    case 2:
        resId = R.layout.splash4;
        break;
    case 3:
        resId = R.layout.splash5;
        break;

    }

    View view = inflater.inflate(resId, null);
    ((ViewPager) collection).addView(view, 0);
    return view;

}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    // TODO Auto-generated method stub
    ((ViewPager) arg0).removeView((View) arg2);

}

@Override
public Parcelable saveState() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    // TODO Auto-generated method stub
    return arg0 == ((View) arg1);
}

}

Logs:

01-17 21:03:35.179: W/dalvikvm(16226): threadid=1: thread exiting with uncaught exception (group=0x401a2560)
01-17 21:03:35.179: E/AndroidRuntime(16226): FATAL EXCEPTION: main
01-17 21:03:35.179: E/AndroidRuntime(16226): java.lang.NullPointerException
01-17 21:03:35.179: E/AndroidRuntime(16226):    at   com.example.survtest2.SplashPager.instantiateItem(SplashPager.java:33)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:801)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.support.v4.view.ViewPager.populate(ViewPager.java:930)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.view.View.measure(View.java:8424)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at  android.view.View.measure(View.java:8424)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at  android.view.View.measure(View.java:8424)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.view.View.measure(View.java:8424)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.view.ViewRoot.performTraversals(ViewRoot.java:844)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1864)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.os.Looper.loop(Looper.java:130)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at android.app.ActivityThread.main(ActivityThread.java:3733)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at java.lang.reflect.Method.invokeNative(Native Method)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at java.lang.reflect.Method.invoke(Method.java:507)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:650)
01-17 21:03:35.179: E/AndroidRuntime(16226):    at dalvik.system.NativeStart.main(Native Method)
01-17 21:03:35.199: E/AndroidRuntime(16226): [Blue Error Handler] Make Debugging Report file for main
01-17 21:03:35.199: E/AndroidRuntime(16226): java.lang.NullPointerException
01-17 21:03:35.199: E/AndroidRuntime(16226):    at com.example.survtest2.SplashPager.instantiateItem(SplashPager.java:33)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:801)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.support.v4.view.ViewPager.populate(ViewPager.java:930)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.view.View.measure(View.java:8424)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at  android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at  android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.view.View.measure(View.java:8424)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.view.View.measure(View.java:8424)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.view.View.measure(View.java:8424)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.view.ViewRoot.performTraversals(ViewRoot.java:844)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1864)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.os.Looper.loop(Looper.java:130)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at android.app.ActivityThread.main(ActivityThread.java:3733)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at java.lang.reflect.Method.invokeNative(Native Method)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at    java.lang.reflect.Method.invoke(Method.java:507)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:650)
01-17 21:03:35.199: E/AndroidRuntime(16226):    at     dalvik.system.NativeStart.main(Native Method)

I’m pretty sure this is the cause of the problem but I don’t know how this method works or how to fix it:

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    // TODO Auto-generated method stub
    ((ViewPager) arg0).removeView((View) arg2);

}

Another question I have is how can I use that button to load a new view or activity? It wont allow me to implement activity and that’s the only way I learned how to call in bundles. Thanks in advance!

  • 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-17T10:31:07+00:00Added an answer on June 17, 2026 at 10:31 am

    Try changing destroyItem to this:

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

    and isViewFromObject to:

    @Override
    public boolean isViewFromObject(View view, Object object){
    
        return view.equals(object); 
     }
    

    If these changes dont work then I suggest u check what happens after u can “findViewById” on the collection View. It looks like the button u are looking is not within that object. You have to search for it in the view you are inflating.

    Try this code for your instantiation code:

    @Override
        public Object instantiateItem(View collection, int position) {
            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            int resId = 0;
            View view = null;
            switch (position) {
    
            case 0:
                view = inflater.inflate(R.layout.splash2, null);
                b = (Button) view.findViewById(R.id.splashB);
                b.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                                            //setContentView(r.layout.someLayout);
                    }
                });
                break;
            case 1:
                view = inflater.inflate(R.layout.splash3, null);
                break;
            case 2:
                view = inflater.inflate(R.layout.splash4, null);
                break;
            case 3:
                view = inflater.inflate(R.layout.splash5, null);
                break;
    
            }
    
    
            ((ViewPager) collection).addView(view, 0);
            return view;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I have been unable to fix a problem with Java Unicode and encoding. The

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.