web-dev-qa-db-fra.com

ViewPager avec des fragments à l'intérieur de PopupWindow (ou DialogFragment) - Erreur aucune vue trouvée pour l'ID du fragment

J'ai créé une FragmentActivity avec ce code ci-dessous

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;
    }

}

et c'est mon 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>

Le FragmentAcoesMusculares (le code à l'intérieur de la méthode getFragments ()) est cette classe ci-dessous

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;
}

}

et c'est mon adaptateur

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();
    }
}

Tout ce code ci-dessus fonctionne parfaitement.

Maintenant, j'essaye de créer un PopupWindow (ou DialogFragment) qui aura mon ViewPager.

J'ai modifié mon code pour cela (maintenant mon XML activity_principal1 a juste un bouton et la nouvelle popup XML a mon 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;
}

}

et enfin c'est l'erreur qui se produit maintenant.

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)

Quelqu'un sait ce que je fais mal?

Merci et salutations.

29
groff07

J'ai trouvé un moyen de résoudre mon problème.

Et c'est parti

J'ai d'abord utilisé DialogFragment à la place de PopupView.

Donc, dans mon activité principale, je n'ai créé qu'un bouton qui appelle mon 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(), "");
            }
        });
    }

}

Mon adaptateur est toujours le même que la question.

Et c'est là que la magie opère.

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;
    }
}

La différence est le getChildFragmentManager (). Ce petit morceau de code m'a sauvé la journée.

L'explication à cela est lorsque j'utilisais getSupportFragmentManager () et que j'indiquais même que le viewpager était dans un autre XML de mise en page qu'il pensait être dans le XML de mise en page principal.

Maintenant, mon application obtient le fragment enfant alors maintenant il voit le ViewPager.

C'est tout.

Merci tout le monde.

49
groff07

J'ai eu le même problème, j'essaie de créer un autre viewpager dans ma boîte de dialogue avec un autre modèle xml, vous savez que je n'ai pas trouvé d'informations pour résoudre ce problème, mais j'utilise une autre implémentation de view pager, qui fonctionne exactement comme le viewpager dans la bibliothèque supporta, est appelé JazzyViewPager, et ont plusieurs animations si mignonnes! Je vous recommande d'utiliser ceci.

Voici le lien: https://github.com/jfeinstein10/JazzyViewPager

1
Ollie Strevel