gpt4 book ai didi

java - 将 viewpager 放在弹出窗口中

转载 作者:行者123 更新时间:2023-11-29 08:42:53 25 4
gpt4 key购买 nike

我正在尝试将 viewpager 放在弹出窗口中。当我当前的代码看起来像这样时。

@Override
protected void onCreate(Bundle savedInstanceState) {

FragmentPagerAdapter adapterViewPager;

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mainLayout = new RelativeLayout(this);
viewPager = new ViewPager(this);

layoutParams = new ViewGroup.LayoutParams(-2, -2);//wrap content

popUp = new PopupWindow(this);

//...

popUp.setContentView(viewPager);

mainLayout.addView(button);

adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapterViewPager);

// Attach the page change listener inside the activity
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

// This method will be invoked when a new page becomes selected.
@Override
public void onPageSelected(int position) {
}

// This method will be invoked when the current page is scrolled
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// Code goes here
}

// Called when the scroll state changes:
// SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
@Override
public void onPageScrollStateChanged(int state) {
// Code goes here
}
});

setContentView(mainLayout);

}//end of oncreate

但是当我尝试运行它时,android 抛出一个 IllegalStateException 错误,说 viewpager 需要一个 viewID。我该如何解决这个问题?

08-11 11:32:13.996 2300-2300/lii2.github.com.popupapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: lii2.github.com.popupapp, PID: 2300
java.lang.IllegalStateException: ViewPager with adapter lii2.github.com.popupapp.MainActivity$MyPagerAdapter@316fcff requires a view id
at android.support.v4.app.FragmentPagerAdapter.startUpdate(FragmentPagerAdapter.java:81)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1117)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1085)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1611)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1191)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

将此添加到您的资源中:

    <item name="viewpager" type="id" />

并调用

    viewPager.setId(R.id.viewpager);

关于java - 将 viewpager 放在弹出窗口中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38900704/

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