-2

I am new in Android. To create a slide I used PageAdapter. Below is my code.

public class MyPagerAdapter extends PagerAdapter {
    @Override
    public int getCount() {
        return 4; //set  number of swipe screens here
    }
    @Override
    public Object instantiateItem(final ViewGroup collection, final int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int resId = 0;
        switch (position) {
            case 0:
                resId = R.layout.activity_slide; //set which layout will show on load
                break;
            case 1:
                resId = R.layout.slide_2; //what layout swiping shows
                break;
            case 2:
                resId = R.layout.slide_3; //what layout swiping shows
                break;
            case 3:
                resId = R.layout.slide_4; //what layout swiping shows
                break;
        }
        View view = inflater.inflate(resId, null);
        ((ViewPager) collection).addView(view, 0);
        return view;
    }
    @Override
    public void destroyItem(final ViewGroup arg0, final int arg1, final Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }
    @Override
    public boolean isViewFromObject(final View arg0, final Object arg1) {
        return arg0 == ((View) arg1);
    }
}

My problem is in layout.slide_4 I placed a Button and I want to open new activity on click of that button. So I wrote below code oncreate:

Button btnExplore = (Button)findViewById(R.id.imageButton);
    btnExplore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View vw) {
            Intent i = new Intent(IntroSlide.this, StartScreen.class);
            startActivity(i);
        }
    });

Logcat

01-18 14:58:58.267 22740-22740/com.totsmart.pedronapp W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x536dd160)
01-18 14:58:58.287 22740-22740/com.totsmart.pedronapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.totsmart.pedronapp, PID: 22740
                                                                    java.lang.NullPointerException
                                                                        at com.totsmart.pedronapp.IntroSlide$MyPagerAdapter.instantiateItem(IntroSlide.java:59)
                                                                        at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870)
                                                                        at android.support.v4.view.ViewPager.populate(ViewPager.java:1086)
                                                                        at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
                                                                        at android.support.v4.view.ViewPager$3.run(ViewPager.java:251)
                                                                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:771)
                                                                        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                                                                        at android.view.Choreographer.doFrame(Choreographer.java:543)
                                                                        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:757)
                                                                        at android.os.Handler.handleCallback(Handler.java:733)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:149)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5045)
                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:515)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
                                                                        at dalvik.system.NativeStart.main(Native Method)

I am getting NullPointerException. Where I am going wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
Vinie
  • 2,983
  • 1
  • 18
  • 29

1 Answers1

3

As per our discussion in comments your layouts are being loaded at runtime. So if any layout is not loaded and you try to use any view of that layout you will get NullPointerException.

I would suggest to add a null check before setting onClickListener like this

Button btnExplore = (Button)findViewById(R.id.imageButton);
if(btnExplore != null)
{
    btnExplore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View vw) {
            Intent i = new Intent(IntroSlide.this, StartScreen.class);
            startActivity(i);
        }
    });
}

Note: You should put all these codes in a separate method and call that method each time you load a new layout.

Update: Better solution would be to divide the listener codes based on individual layouts and keep different method for different layout's view listeners. After this, call appropriate method from your switch statement. Also you will have to move the following code in individual cases.

View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57