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!

Jason Cheladyn
  • 565
  • 1
  • 12
  • 24
  • by looking at the log i can only say that the nullpointerexception fires in instantiateItem method and it would be easier if you can post the line numbers of the class too. – sasankad Jan 18 '13 at 02:22
  • THIS IS LINE 35 b.setOnClickListener(new OnClickListener() { how do I add the line number? – Jason Cheladyn Jan 18 '13 at 02:24
  • i suppose only way is to post the line no on each line of the code... but anyway that's not necessary. according to the log nullpointerexception occurs at line 33 in the splashPage class. could you point out that line? – sasankad Jan 18 '13 at 02:29
  • b.setOnClickListener(new OnClickListener() { – Jason Cheladyn Jan 18 '13 at 02:50
  • I may be wrong but as i understand is nullpointerexception occurs due to button being null. – sasankad Jan 18 '13 at 03:24

1 Answers1

1

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;
    }
Marco RS
  • 8,145
  • 3
  • 37
  • 45
  • It stopped crashing!! Could you please explain this code for me? It stopped crashing but now it the 3rd view (case 2) turns into a whitescreen after a second. This is great thou! – Jason Cheladyn Jan 18 '13 at 03:34
  • Changing the destroyItem and isViewFromObject back to normal removed the white screen. Could you please explain this code to me! Could you tell me how to use the button to setContentView? It won't let me implement Activity. Thank you so much – Jason Cheladyn Jan 18 '13 at 03:45
  • I changed your code to where it has to inflate the views for every page first before using the method findViewById. You were searching for the button in the wrong view. As for the button, you want to launch a activity based on the button click? – Marco RS Jan 18 '13 at 03:51
  • Yes, but I don't know how to access the intent method. – Jason Cheladyn Jan 18 '13 at 03:53
  • Your views hold a reference to a context. You can call view.getContext().startActivity(yourIntent); – Marco RS Jan 18 '13 at 03:54
  • Thanks a lot man, i've been working on this forever now I can move on to the hard part haha. – Jason Cheladyn Jan 18 '13 at 04:01