Question

I am trying to use the library: com.edmodo.rangebar.RangeBar with the ViewPager but It gives a nullpointerexception when I try to add it into the viewpager. But when I use it outside the ViewPager there is no problem. And fragments are no option because you cannot nest fragments in a viewpager that is in a fragment.

<android.support.v4.view.ViewPager
     android:layout_width="match_parent"
     android:layout_height="wrap_content">

     <LinearLayout
          android:id="@+id/ll_filter_seekbar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal">


                <com.edmodo.rangebar.RangeBar
                    xmlns:custom="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/rangebar"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    custom:tickCount="2000"
                    custom:tickHeight="0dp" />


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

Error:

java.lang.NullPointerException
    at com.edmodo.rangebar.RangeBar.onDraw(RangeBar.java:265)
    at android.view.View.draw(View.java:14465)
    at android.view.View.draw(View.java:14350)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
    at android.view.View.draw(View.java:14348)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
    at android.view.View.draw(View.java:14468)
    at android.support.v4.view.ViewPager.draw(ViewPager.java:2171)
    at android.view.View.draw(View.java:14350)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
    at android.view.View.draw(View.java:14348)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
    at android.view.View.draw(View.java:14468)
    at android.widget.FrameLayout.draw(FrameLayout.java:472)
    at android.widget.ScrollView.draw(ScrollView.java:1603)
    at android.view.View.draw(View.java:14350)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
    at android.view.View.draw(View.java:14468)
    at android.widget.FrameLayout.draw(FrameLayout.java:472)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:584)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:338)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:332)
    at com.android.tools.idea.rendering.RenderService$3.compute(RenderService.java:560)
    at com.android.tools.idea.rendering.RenderService$3.compute(RenderService.java:549)
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:932)
    at com.android.tools.idea.rendering.RenderService.createRenderSession(RenderService.java:549)
    at com.android.tools.idea.rendering.RenderService.render(RenderService.java:622)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:585)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1900(AndroidLayoutPreviewToolWindowManager.java:80)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$6$1.run(AndroidLayoutPreviewToolWindowManager.java:527)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:178)
    at com.intellij.openapi.progress.ProgressManager.executeProcessUnderProgress(ProgressManager.java:209)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:212)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:171)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$6.run(AndroidLayoutPreviewToolWindowManager.java:522)
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
    at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
    at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
    at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
    at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
    at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
    at com.intellij.util.Alarm$Request$1.run(Alarm.java:327)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

The code from the PageAdapter:

public class FilterPagerAdapter extends PagerAdapter {

private static final int NUM_PAGES = 1;

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    int resId = 0;
    Tools.LOG_DEBUG("This page: " + position);
    switch (position) {
        case 0:
            resId = R.id.page_one;
            break;
    }
    return container.findViewById(resId);
}

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

Solution

A ViewPager adds its children(the pages) through it's adapter, you shouldn't add the child views directly in the xml layout as you did. So, keep the initial layout with only the ViewPager:

<android.support.v4.view.ViewPager
     android:layout_width="match_parent"
     android:layout_height="wrap_content" />

create a new xml layout with the content of the page(like page.xml):

<LinearLayout
      android:id="@+id/ll_filter_seekbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">


            <com.edmodo.rangebar.RangeBar
                xmlns:custom="http://schemas.android.com/apk/res-auto"
                android:id="@+id/rangebar"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                custom:tickCount="2000"
                custom:tickHeight="0dp" />


</LinearLayout>

and wire this up in the adapter by getting a reference to a LayoutInflater(either pass it to the adapter directly or create it from a Context)and then using that to inflate the above page.xml layout according to your position.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top