3

In a one screen I am using two viewPagers - one for image autoscroling and other for buttons hand scrolling. Image autoscrolling was working perfectly fine, but when I added implementation for buttons on viewPager, my application started crashing for nullpointer on setAdapter, I am using fragmentPagerAdapter. For images autoslider I am using this reference code by https://github.com/JakeWharton/Android-ViewPagerIndicator

Here is my source code

layout xml file simple_circles.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <!-- android:layout_weight="0.01" -->

    <SearchView
        android:id="@+id/search_view"
        android:layout_width="fill_parent"
        android:layout_height="80dp" >
    </SearchView>
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="400dp">
    </android.support.v4.view.ViewPager>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager_for_buttons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >       

    </android.support.v4.view.ViewPager>

fragment for adding buttons on slider -

    public class ScreenSlidePageFragment extends Fragment {

    public static final String ARG_PAGE = "page";


    private int mPageNumber;


    public static Fragment create(int pageNumber) {
        ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, pageNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public ScreenSlidePageFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mPageNumber = getArguments().getInt(ARG_PAGE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         // Inflate the layout containing a title and body text.
        Button music_button = new Button(getActivity());
        music_button.setText("Music");

        Button news_button = new Button(getActivity());
        news_button.setText("News");

        Button videos_button = new Button(getActivity());
        videos_button.setText("Videos");
        Button photos_button = new Button(getActivity());
        photos_button.setText("Photos");
        LinearLayout layout = new LinearLayout(getActivity());
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        //layout.setGravity(Gravity.CENTER);
        layout.addView(music_button);
        layout.addView(news_button);
        layout.addView(videos_button);
        layout.addView(photos_button);

        return layout;



    }


    public int getPageNumber() {
        return mPageNumber;
    }
}

Activity class and adapter for viewPager -

public class SampleCirclesDefault extends FragmentActivity {

    // Initialization

    TestFragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mIndicator;
    Bitmap bitmap;
    ArrayList<String> bandImagesList;
    ImageView build_bandapp_img;
    boolean pagerMoved;
    Runnable Update;
    Handler h;
    long ANIM_VIEWPAGER_DELAY;
    Handler handler;
    int NUM_PAGES = 5;
    ViewPager buttonPager;
    ScreenSlidePagerAdapter buttonAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // No action bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
            **setContentView(R.layout.simple_circles);**
        build_bandapp_img = (ImageView) findViewById(R.id.imageView_for_webview);
        build_bandapp_img.setOnClickListener(new OnClickListener() {
            // Build bandapp button
            @Override
            public void onClick(View v) {

                Intent i = new Intent(SampleCirclesDefault.this,
                        BandDetailedActivity.class);
                startActivity(i);
            }
        });

        mAdapter = new TestFragmentAdapter(getSupportFragmentManager());// adapter
                                                                        // for
                                                                        // viewpager

        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
        buttonAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        //buttonAdapter = new AdapterForFragmentOfButtonSlider(getSupportFragmentManager());
        buttonPager = (ViewPager)findViewById(R.id.pager_for_buttons);
        buttonPager.setAdapter(buttonAdapter); //***error -NullPointerException***//

        mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);// dot dot indicator
        buttonPager.setCurrentItem(1); 
        handler = new Handler();
        // autoscrolling of view pager
        Update = new Runnable() {

            int currentPage = mPager.getCurrentItem();

            public void run() {
                if (currentPage == NUM_PAGES - 1) {
                    currentPage = 0;
                }
                mPager.setCurrentItem(currentPage++, true);
            }
        };

        Timer swipeTimer = new Timer();
        swipeTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                handler.post(Update);
            }
        }, 100, 5000);

    }

     private class ScreenSlidePagerAdapter extends FragmentPagerAdapter {
            public ScreenSlidePagerAdapter(FragmentManager fragmentManager) {
                super(fragmentManager);
            }

            @Override
            public Fragment getItem(int position) {
                return ScreenSlidePageFragment.create(position);
            }

            @Override
            public int getCount() {
                return NUM_PAGES;
            }
        }
    }

Stack trace

01-23 13:56:57.067: W/dalvikvm(18861): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
01-23 13:56:57.098: E/AndroidRuntime(18861): FATAL EXCEPTION: main
01-23 13:56:57.098: E/AndroidRuntime(18861): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.viewpagerindicator.sample/com.viewpagerindicator.sample.SampleCirclesDefault}: java.lang.NullPointerException
01-23 13:56:57.098: E/AndroidRuntime(18861):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-23 13:56:57.098: E/AndroidRuntime(18861):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-23 13:56:57.098: E/AndroidRuntime(18861):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-23 13:56:57.098: E/AndroidRuntime(18861):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-23 13:56:57.098: E/AndroidRuntime(18861):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 13:56:57.098: E/AndroidRuntime(18861):    at android.os.Looper.loop(Looper.java:137)
01-23 13:56:57.098: E/AndroidRuntime(18861):    at android.app.ActivityThread.main(ActivityThread.java:5041)
01-23 13:56:57.098: E/AndroidRuntime(18861):    at java.lang.reflect.Method.invokeNative(Native Method)
01-23 13:56:57.098: E/AndroidRuntime(18861):    at java.lang.reflect.Method.invoke(Method.java:511)
01-23 13:56:57.098: E/AndroidRuntime(18861):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-23 13:56:57.098: E/AndroidRuntime(18861):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-23 13:56:57.098: E/AndroidRuntime(18861):    at dalvik.system.NativeStart.main(Native Method)
01-23 13:56:57.098: E/AndroidRuntime(18861): Caused by: java.lang.NullPointerException
01-23 13:56:57.098: E/AndroidRuntime(18861):    at com.viewpagerindicator.sample.SampleCirclesDefault.onCreate(SampleCirclesDefault.java:85)
01-23 13:56:57.098: E/AndroidRuntime(18861):    at android.app.Activity.performCreate(Activity.java:5104)
01-23 13:56:57.098: E/AndroidRuntime(18861):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-23 13:56:57.098: E/AndroidRuntime(18861):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-23 13:56:57.098: E/AndroidRuntime(18861):    ... 11 more
01-23 13:57:13.356: I/Process(18861): Sending signal. PID: 18861 SIG: 9

I have pointed exactly where I am getting error.

Please help me to resolve this error, thanks in advance.

11
  • Either your buttonPageror buttonAdapteris null, I think your buttonAdapteris null. Be sure that it is instantiated correctly. Commented Jan 23, 2014 at 12:59
  • yes I also feel same, but I dont find where the problem is but I wasted whole day in this and coldnt find what is wrong Commented Jan 23, 2014 at 13:06
  • 1
    @njzk2 also the layout posted does not have a imageview and the id referenced seems to be right for buttonPager. Commented Jan 23, 2014 at 16:19
  • 1
    @longwalker : if the adapter were null, the exception would not occur at this specific line, but rather inside the setAdapter method. Commented Jan 23, 2014 at 16:47
  • 1
    yes it was because of different layout files thank you for help Commented Jan 23, 2014 at 17:06

1 Answer 1

1
yes it was because of different layout files

So if buttonPager is null you have set a layout that does not have a buttonPager.

This confirms what i had commented earlier.

  buttonPager = (ViewPager)findViewById(R.id.pager_for_buttons);
  buttonPager.setAdapter(buttonAdapter); 

Also check this

http://developer.android.com/training/animation/screen-slide.html

Sign up to request clarification or add additional context in comments.

16 Comments

I have added setContentView(R.layout.simple_circles); this line but deleted it here with some commented code its there in the application
@user3153020 then post the stack trace also and update your post. also indicate the name of the xml correctly
moreover, the lack of setContentView would have resulted in a much earlier crash, probably at build_bandapp_img.setOnClickListener
buttonAdapter is null
It was because of layout large and layout folder, had different versions of same file
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.