gpt4 book ai didi

java - com.edmodo.rangebar.RangeBar 与 viewpager

转载 作者:行者123 更新时间:2023-11-30 02:55:00 26 4
gpt4 key购买 nike

我正在尝试将库:co​​m.edmodo.rangebar.RangeBar 与 ViewPager 一起使用,但是当我尝试将它添加到 viewpager 时,它给出了一个空指针异常。但是当我在 ViewPager 之外使用它时没有问题。并且 fragment 不是选项,因为您不能将 fragment 嵌套在 fragment 中的 viewpager 中。

<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>

错误:

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)

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);
}
}

最佳答案

ViewPager 通过它的适配器添加它的 subview (页面),你不应该像你那样直接在 xml 布局中添加 subview 。因此,只保留 ViewPager 的初始布局:

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

使用页面内容创建一个新的 xml 布局(如 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>

并通过获取对 LayoutInflater 的引用(将其直接传递给适配器或从 Context 创建它)然后使用它,将其连接到适配器中根据您的位置膨胀上面的 page.xml 布局。

关于java - com.edmodo.rangebar.RangeBar 与 viewpager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23429076/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com