2016-08-11 113 views
0

我想把一個viewpager放在一個彈出窗口中。當我目前的代碼看起來像這樣。把一個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 [email protected] 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) 

回答

3

添加到您的資源:

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

,並呼籲

viewPager.setId(R.id.viewpager); 
+0

我想這一點,但Android的工作室稱它 「無法解決方法SETID(INT)」,即使在清潔和重建項目。 –

+0

請注意,「setId」中的「d」是小寫字母。 –

+0

這解決了這個問題,謝謝。 –