33

I created a FragmentActivity with this code below

public class Activity_principal1 extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal1);

        List<Fragment> fragments = getFragments();
        AcoesMuscularesAdapter ama = new AcoesMuscularesAdapter(getSupportFragmentManager(), fragments);
        ViewPager vp_contentAcoesMusculares_SequenciaExercicios = (ViewPager) findViewById(R.id.vp_contentAcoesMusculares_SequenciaExercicios);
        vp_contentAcoesMusculares_SequenciaExercicios.setAdapter(ama);

    }

    private List<Fragment> getFragments(){
        List<Fragment> fList = new ArrayList<Fragment>();
        fList.add(FragmentAcoesMusculares.newInstance("Fragment 1",1));
        fList.add(FragmentAcoesMusculares.newInstance("Fragment 2",2));
        fList.add(FragmentAcoesMusculares.newInstance("Fragment 3",3));
        return fList;
    }

}

and this is my XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/framelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white" >

<android.support.v4.view.ViewPager
    android:id="@+id/vp_contentAcoesMusculares_SequenciaExercicios"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:background="#fff" />

</RelativeLayout>

The FragmentAcoesMusculares (the code inside the method getFragments()) is this class below

public class FragmentAcoesMusculares extends Fragment{

    public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";

    public static final FragmentAcoesMusculares newInstance(String message,int item)
    {
        FragmentAcoesMusculares f = new FragmentAcoesMusculares();
        Bundle bdl = new Bundle(item);
        f.item = item;
        bdl.putString(EXTRA_MESSAGE, message);
        f.setArguments(bdl);
        return f;
    }


    private int item;
    private ImageView images;
    private String urlString = "";
    int i = 0;;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         final View v = inflater.inflate(R.layout.linha_acoesmusculares_viewpager, container, false);

         images = (ImageView) v.findViewById(R.id.images);
         final Runnable r = new Runnable() {

            @Override
            public void run() {
            if(i == 0){
                urlString = "http://54.232.207.226/atlas.limittraining.com.br/thumb/grupo-muscular-sequencia/1/0/0/bb91e662b4fd6ffde74757ff9bd6e108.jpg";
            }else{
                urlString = "http://54.232.207.226/atlas.limittraining.com.br/thumb/grupo-muscular-sequencia/1/0/0/7730258ca72a75365d0c4b92a1a68a0a.jpg";
            }

            AQuery aquery = new AQuery(images);
            aquery.image(urlString);
            i++;
            if(i >= 2){
                i = 0;
            }
            images.postDelayed(this, 1500);
        }
    };
    images.postDelayed(r, 1500);

    return v;
}

}

and this is my Adapter

public class AcoesMuscularesAdapter extends FragmentPagerAdapter{

    private List<Fragment> fragments;

    public AcoesMuscularesAdapter(FragmentManager fm, List<Fragment> fragments){
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }
}

All this code above it's working perfectly.

Now I'm trying to create a PopupWindow (or DialogFragment) who will have my ViewPager.

I modified my code for this (now my XML activity_principal1 have just a Button and the new XML popup have my ViewPager)

public class Activity_principal1 extends FragmentActivity {

private LayoutInflater inflater;
private View view;
private PopupWindow window;
private Button abrir;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_principal1);

    List<Fragment> fragments = getFragments();
    AcoesMuscularesAdapter ama = new AcoesMuscularesAdapter(getSupportFragmentManager(), fragments);

    inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    view = inflater.inflate(R.layout.popup, null,true);

    ViewPager vp_contentAcoesMusculares_SequenciaExercicios = (ViewPager) view.findViewById(R.id.vp_contentAcoesMusculares_SequenciaExercicios);
    vp_contentAcoesMusculares_SequenciaExercicios.setAdapter(ama);

    window = new PopupWindow(view);

    abrir = (Button) findViewById(R.id.abrir);
    abrir.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            window.showAsDropDown(v);
        }
    });
}

private List<Fragment> getFragments(){
    List<Fragment> fList = new ArrayList<Fragment>();
        fList.add(FragmentAcoesMusculares.newInstance("Fragment 1",1));
        fList.add(FragmentAcoesMusculares.newInstance("Fragment 2",2));
        fList.add(FragmentAcoesMusculares.newInstance("Fragment 3",3));
    return fList;
}

}

and FINALLY, this is the error that is occurring now.

10-23 12:29:39.273: E/FragmentManager(6062): No view found for id 0x7f080005 (br.com.examples.testeprojects:id/vp_contentAcoesMusculares_SequenciaExercicios) for fragment FragmentAcoesMusculares{419e41d0 #0 id=0x7f080005 android:switcher:2131230725:0}
10-23 12:29:39.273: E/FragmentManager(6062): Activity state:
10-23 12:29:39.273: D/FragmentManager(6062):   Local FragmentActivity 419d8e78 State:
10-23 12:29:39.281: D/FragmentManager(6062):     mCreated=truemResumed=true mStopped=false mReallyStopped=false
10-23 12:29:39.281: D/FragmentManager(6062):     mLoadersStarted=true
10-23 12:29:39.281: D/FragmentManager(6062):   Active Fragments in 419d9f78:
10-23 12:29:39.281: D/FragmentManager(6062):     #0: FragmentAcoesMusculares{419e41d0 #0 id=0x7f080005 android:switcher:2131230725:0}
10-23 12:29:39.281: D/FragmentManager(6062):       mFragmentId=#7f080005 mContainerId=#7f080005 mTag=android:switcher:2131230725:0
10-23 12:29:39.281: D/FragmentManager(6062):       mState=1 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
10-23 12:29:39.281: D/FragmentManager(6062):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
10-23 12:29:39.281: D/FragmentManager(6062):       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
10-23 12:29:39.281: D/FragmentManager(6062):       mRetainInstance=false mRetaining=false mUserVisibleHint=true
10-23 12:29:39.281: D/FragmentManager(6062):       mFragmentManager=FragmentManager{419d9f78 in Activity_principal1{419d8e78}}
10-23 12:29:39.289: D/FragmentManager(6062):       mActivity=br.com.examples.testeprojects.Activity_principal1@419d8e78
10-23 12:29:39.289: D/FragmentManager(6062):       mArguments=Bundle[{EXTRA_MESSAGE=Fragment 1}]
10-23 12:29:39.289: D/FragmentManager(6062):     #1: FragmentAcoesMusculares{419e4450 #1 id=0x7f080005 android:switcher:2131230725:1}
10-23 12:29:39.289: D/FragmentManager(6062):       mFragmentId=#7f080005 mContainerId=#7f080005 mTag=android:switcher:2131230725:1
10-23 12:29:39.289: D/FragmentManager(6062):       mState=0 mIndex=1 mWho=android:fragment:1 mBackStackNesting=0
10-23 12:29:39.289: D/FragmentManager(6062):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
10-23 12:29:39.289: D/FragmentManager(6062):       mHidden=false mDetached=false mMenuVisible=false mHasMenu=false
10-23 12:29:39.289: D/FragmentManager(6062):       mRetainInstance=false mRetaining=false mUserVisibleHint=false
10-23 12:29:39.296: D/FragmentManager(6062):       mFragmentManager=FragmentManager{419d9f78 in Activity_principal1{419d8e78}}
10-23 12:29:39.296: D/FragmentManager(6062):       mArguments=Bundle[{EXTRA_MESSAGE=Fragment 2}]
10-23 12:29:39.296: D/FragmentManager(6062):   Added Fragments:
10-23 12:29:39.296: D/FragmentManager(6062):     #0: FragmentAcoesMusculares{419e41d0 #0 id=0x7f080005 android:switcher:2131230725:0}
10-23 12:29:39.296: D/FragmentManager(6062):     #1: FragmentAcoesMusculares{419e4450 #1 id=0x7f080005 android:switcher:2131230725:1}
10-23 12:29:39.296: D/FragmentManager(6062):   FragmentManager misc state:
10-23 12:29:39.296: D/FragmentManager(6062):     mActivity=br.com.examples.testeprojects.Activity_principal1@419d8e78
10-23 12:29:39.296: D/FragmentManager(6062):     mContainer=android.support.v4.app.FragmentActivity$2@419da8d0
10-23 12:29:39.296: D/FragmentManager(6062):     mCurState=5 mStateSaved=false mDestroyed=false
10-23 12:29:39.296: D/FragmentManager(6062):   View Hierarchy:
10-23 12:29:39.296: D/FragmentManager(6062):     com.android.internal.policy.impl.PhoneWindow$DecorView{419db990 V.E..... ... 0,0-600,976}
10-23 12:29:39.296: D/FragmentManager(6062):       android.widget.LinearLayout{419dd410 V.E..... ... 0,0-600,976}
10-23 12:29:39.296: D/FragmentManager(6062):         android.view.ViewStub{419de850 G.E..... ... 0,0-0,0 #10203e5}
10-23 12:29:39.304: D/FragmentManager(6062):         android.widget.FrameLayout{419dec20 V.E..... ... 0,0-600,976 #1020002 android:id/content}
10-23 12:29:39.304: D/FragmentManager(6062):           android.widget.RelativeLayout{419dfe98 V.E..... ... 0,0-600,976 #7f080000 app:id/framelayout}
10-23 12:29:39.304: D/FragmentManager(6062):             android.widget.Button{419e0618 VFED..C. ... 0,0-73,48 #7f080002 app:id/abrir}
10-23 12:29:39.304: W/System.err(6062): java.lang.IllegalArgumentException: No view found for id 0x7f080005 (br.com.examples.testeprojects:id/vp_contentAcoesMusculares_SequenciaExercicios) for fragment FragmentAcoesMusculares{419e41d0 #0 id=0x7f080005 android:switcher:2131230725:0}
10-23 12:29:39.304: W/System.err(6062):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
10-23 12:29:39.304: W/System.err(6062):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-23 12:29:39.304: W/System.err(6062):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-23 12:29:39.304: W/System.err(6062):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-23 12:29:39.304: W/System.err(6062):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
10-23 12:29:39.312: W/System.err(6062):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
10-23 12:29:39.312: W/System.err(6062):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
10-23 12:29:39.312: W/System.err(6062):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
10-23 12:29:39.312: W/System.err(6062):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
10-23 12:29:39.312: W/System.err(6062):     at android.view.View.measure(View.java:15479)
10-23 12:29:39.312: W/System.err(6062):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
10-23 12:29:39.312: W/System.err(6062):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
10-23 12:29:39.312: W/System.err(6062):     at android.view.View.measure(View.java:15479)
10-23 12:29:39.312: W/System.err(6062):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1968)
10-23 12:29:39.312: W/System.err(6062):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1214)
10-23 12:29:39.312: W/System.err(6062):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1387)
10-23 12:29:39.312: W/System.err(6062):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
10-23 12:29:39.312: W/System.err(6062):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4464)
10-23 12:29:39.312: W/System.err(6062):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
10-23 12:29:39.320: W/System.err(6062):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
10-23 12:29:39.320: W/System.err(6062):     at android.view.Choreographer.doFrame(Choreographer.java:525)
10-23 12:29:39.320: W/System.err(6062):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
10-23 12:29:39.320: W/System.err(6062):     at android.os.Handler.handleCallback(Handler.java:615)
10-23 12:29:39.320: W/System.err(6062):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 12:29:39.320: W/System.err(6062):     at android.os.Looper.loop(Looper.java:137)
10-23 12:29:39.320: W/System.err(6062):     at android.app.ActivityThread.main(ActivityThread.java:4895)
10-23 12:29:39.320: W/System.err(6062):     at java.lang.reflect.Method.invokeNative(Native Method)
10-23 12:29:39.320: W/System.err(6062):     at java.lang.reflect.Method.invoke(Method.java:511)
10-23 12:29:39.320: W/System.err(6062):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
10-23 12:29:39.328: W/System.err(6062):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
10-23 12:29:39.328: W/System.err(6062):     at dalvik.system.NativeStart.main(Native Method)

Does someone know what I doing wrong?

Thanks and regards.

groff07
  • 2,363
  • 3
  • 32
  • 48
  • I'm not sure if it still helps, but there's a tutorial of how you put ViewPager in a Dialog here:https://github.com/kiteflo/Android_Tabbed_Dialog – Hendra Anggrian May 09 '15 at 08:33

2 Answers2

53

I found a way to my problem.

Here we go

First I used DialogFragment instead of PopupView.

So in my main activity, I only created a Button who calls my DialogFragment.

public class Activity_principal1 extends FragmentActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal1);

        Button abrir = (Button) findViewById(R.id.botao);
        abrir.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                new DialogFragmentWindow().show(getSupportFragmentManager(), "");
            }
        });
    }

}

My adapter still the same as the question.

And here is where the magic occurs.

public class DialogFragmentWindow extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.popup, container);

        ViewPager vp_contentAcoesMusculares_SequenciaExercicios = (ViewPager) view.findViewById(R.id.vp_contentAcoesMusculares_SequenciaExercicios);
        List fragments = getFragments();
        AcoesMuscularesAdapter ama = new AcoesMuscularesAdapter(getChildFragmentManager(), fragments);
        vp_contentAcoesMusculares_SequenciaExercicios.setAdapter(ama);

        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return view;
    }

    private List getFragments(){
        List fList = new ArrayList();
            fList.add(FragmentAcoesMusculares.newInstance("Fragment 1",1));
            fList.add(FragmentAcoesMusculares.newInstance("Fragment 2",2));
            fList.add(FragmentAcoesMusculares.newInstance("Fragment 3",3));
        return fList;
    }
}

The difference is the getChildFragmentManager(). This little piece of code saved my day.

The explanation to this is when I was using getSupportFragmentManager() and even indicating the view pager was in another Layout XML he thought being in the main Layout XML.

Now my app gets the child fragment so now he sees the ViewPager.

That's it.

Thanks, everyone.

groff07
  • 2,363
  • 3
  • 32
  • 48
  • 5
    Thank you so very much for this, you were my saviour :). Too bad I only have one upvote. The getChildFragmentManager indeed works like a charm. – kufi Nov 26 '13 at 09:45
  • 2
    What @kufi said. Thanks so much. – Nic Wise Mar 05 '14 at 22:14
  • is there a method show() in DialogFragmentWindow? Because when i add " new DialogFragmentWindow().show(getSupportFragmentManager(), "");" it asked me to create a method called "show()" – Zen Apr 03 '14 at 23:07
  • 2
    Thanks, i was trying to use the supportFragmentManager from the activity, changing it to getChildFragmentManager saved my ass, thanks – Kokusho Aug 26 '14 at 21:40
  • 1
    Thank you it was quite obvious to use it , but nothing in the official docs. – ar-g Jul 09 '15 at 10:52
  • 1
    Works like a charm . Will realy like to know why it wont work for a ViewPager . – Salil Kaul Mar 03 '16 at 08:55
  • 1
    since morning i was looking for the solution and after wasting 4 hours found your solution and it worked like a charm. thanks for posting this question and answer. @groff07 – Däñish Shärmà Jul 20 '16 at 11:31
1

I had the same issue, I try to create another viewpager in my dialog with another xml template, you know i didnt found information for fix this, but i use another view pager implementation, that works exactly as the viewpager in the supporta library, is called JazzyViewPager, and have multiple animations so cute! I recommend you use, this.

Here is the link: https://github.com/jfeinstein10/JazzyViewPager

Ollie Strevel
  • 861
  • 1
  • 14
  • 27
  • 1
    I forgot tell you that this implementation is compatible with CirclePageIndicator ;)) – Ollie Strevel Oct 23 '13 at 14:53
  • Thanks for the answer @olliestrevel. I found another solution and I will put the solution here. Anyway I will look this project you indicated. Can be useful. – groff07 Oct 23 '13 at 20:48
  • 1
    Oh! you are right @groff07! how can i forget the getChildFragmentManager function, that's the solution! however the solution that i suggest you is when you already use the getChildFragmentManager and need another viewpager with modifying the previous Viewpager. – Ollie Strevel Oct 24 '13 at 17:08
  • unrelated, but that's a nice ViewPager animation there – Hendra Anggrian May 09 '15 at 08:34