我正在尝试将一个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)
答案 0 :(得分:4)
将此添加到您的资源:
<item name="viewpager" type="id" />
并致电
viewPager.setId(R.id.viewpager);