Chi-Young Jeffrey Lii Chi-Young Jeffrey Lii - 3 months ago 80
Android Question

Put a viewpager in a popup window

I am trying to put a viewpager in a popup window. When My current code looks like this.

@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


But when I try to run it android throws a IllegalStateException error, saying that viewpager requires a viewID. How do I resolve this?

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)

Answer

Add this to your resources:

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

and call

    viewPager.setId(R.id.viewpager);