fragments in viewpager, no view found error

前端 未结 8 1178
面向向阳花
面向向阳花 2020-12-19 09:23

I have an activity holding a fragment, in this fragment there is a button , when it is clicked, a dialog is popped out.

In this dialog, there is a Viewpager, which

相关标签:
8条回答
  • 2020-12-19 10:02

    getChildFragmentManager() is available since API 17 while you're using the v4 support library. Try using the support fragment manager instead:

    PagerDialog dialog = new PagerDialog(getActivity(),
            getActivity().getSupportFragmentManager());
    
    0 讨论(0)
  • 2020-12-19 10:02

    so far, we can't figure out why there is an error, but I have a way to work around this, using this tip:

    Tip: If you want a custom dialog, you can instead display an Activity as a dialog instead of using the Dialog APIs. Simply create an activity and set its theme to Theme.Holo.Dialog in the manifest element:

    <activity android:theme="@android:style/Theme.Holo.Dialog"
    

    Based on this tip, I don't make PagerDialog extend Dialog but from Fragment. And then put this Fragment inside an Activity , set the theme of this activity as above in AndroidManifest.xml.

    The updated code for the PagerDialog is as followed.

    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentStatePagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class PagerDialog extends Fragment {
    
    ViewPager mViewPager;
    FragmentStatePagerAdapter mAdapter;
    
    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.id.dialog, container, false);
        mViewPager = (ViewPager) v.findViewById(R.id.pager);
        mAdapter = new MyAdapter(getFragmentManager());
        mViewPager.setAdapter(mAdapter);
        return v;
    }
    
    private class MyAdapter extends FragmentStatePagerAdapter {
    
        public MyAdapter(FragmentManager fm) {
            super(fm);
    
        }
    
        @Override
        public Fragment getItem(int index) {
            return new DummyFragment();
        }
    
        @Override
        public int getCount() {
            return 2;
        }
    
    }
    
    private class DummyFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater,
                @Nullable ViewGroup container,
                @Nullable Bundle savedInstanceState) {
    
            View v = inflater.inflate(R.layout.fragment_dummy_layout,
                    container, false);
            return v;
        }
    }
    }
    

    If you have any better solutions or you can find out why the original code in my question does not work, please teach me. I am a newbie in Android programing and I am happy to learn from you all.

    Cheers.

    0 讨论(0)
  • 2020-12-19 10:16

    I think you used

    Dialog dialog = new Dialog(context);
    

    Instends this.

    1st Step: Used DialogFragment

    public class MyCustomDialog extends DialogFragment {
        Button mButton;
        EditText mEditText;
    
    
        List<MasterPageModel> listdata;
        int position;
        ViewPager viewpager;
    
        public MyCustomDialog(List<MasterPageModel> listdata, int position) {
            this.listdata = listdata;
            this.position = position;
        }
    
    
    
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            final View dialog = inflater.inflate(R.layout.dailoglayout, container);
    
            final Dialog dailog = getDialog();
            dailog.getWindow().setLayout(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            dailog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            dailog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
            ImageView closebtn = (ImageView) dialog.findViewById(R.id.closeimgeview);
            closebtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dailog.dismiss();
                }
            });
    
            viewpager = (ViewPager) dialog.findViewById(R.id.dailog_viewpager);
    
            **viewpager.setAdapter(new PagerAdapter(getChildFragmentManager(), listdata));** //This Line is Very important
            viewpager.setCurrentItem(position);
    
            return dialog;
        }
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen); //For Full Screen of DailogBox
        }
    

    }

    2nd Step :set The FragmentViewPager

    public class PagerAdapter extends FragmentPagerAdapter   {
    
        private final List<MasterPageModel> pages;
    
        public PagerAdapter(FragmentManager fm, List<MasterPageModel> pages) {
            super(fm);
            this.pages = pages;
        }
    
    
    
        @Override
        public Fragment getItem(int position) {
            return new ViewPagerFragment(pages.get(position));
        }
    
        @Override
        public int getCount() {
            return pages.size();
        }
    }
    

    3rd Step : Fragment View For ViewPager:

    public class ViewPagerFragment extends Fragment {
    
        MasterPageModel masteDetailModel;
    
    
        public ViewPagerFragment(MasterPageModel questionItem) {
            this.masteDetailModel = questionItem;
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.pagerlayout, container, false);
    
            ImageView image = (ImageView) v.findViewById(R.id.pager_imageview);
            Log.i("Image", "=>" + masteDetailModel.imagelarge);
            Picasso.with(getActivity()).load(masteDetailModel.imagethumb).error(R.drawable.img).into(image);
    
            return v;
        }
    

    4th How to Use this dialogfragment:

     MyCustomDialog fragment1 = new MyCustomDialog(listdata, position);
                    EditionDetailActivity act = (EditionDetailActivity) context;
                    FragmentManager fm = act.getSupportFragmentManager();
                    fragment1.show(fm, "");
    
    0 讨论(0)
  • 2020-12-19 10:18

    I have found a solution to the problem and have modified your classes so that this error no longer occurs.

    The only major difference was that you should use a DialogFragment instead of a Dialog, that way you have access to call getChildFragmentManager() and receive the correct FragmentManager from the DialogFragment.

    Even though you were using getChildFragmentManager() before, it was coming from MyFragment and the PagerDialog class was not a child fragment in MyFragment.

    I have tested the code below, and it should be working fine now.

    MyFragment

    public class MyFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_sandbox, container, false);
            Button button = (Button) v.findViewById(R.id.button);
    
            button.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    DialogFragment newFragment = PagerDialog.newInstance();
                    newFragment.show(getChildFragmentManager(), "dialog");
                }
    
            });
    
            return v;
        }
    }
    

    PagerDialog

    public class PagerDialog extends DialogFragment {
    
        public static PagerDialog newInstance() {
            return new PagerDialog();
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.dialog_fragment, container, false);
    
            ViewPager mViewPager = (ViewPager) v.findViewById(R.id.view_pager);
    
            /* Use childFragmentManager here provided from the PagerDialog */
            MyAdapter mAdapter = new MyAdapter(getChildFragmentManager());
            mViewPager.setAdapter(mAdapter);
    
            return v;
        }
    
        private class MyAdapter extends FragmentStatePagerAdapter {
    
            public MyAdapter(FragmentManager fm) {
                super(fm);
            }
    
            @Override
            public Fragment getItem(int index) {
                return new DummyFragment();
            }
    
            @Override
            public int getCount() {
                return 2;
            }
    
        }
    }
    

    DummyFragment

    public class DummyFragment extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            View v = inflater.inflate(R.layout.fragment_dummy_layout, container, false);
            return v;
        }
    
    }
    

    fragment_sandbox.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test Fragment Dialog Pager"
            android:textSize="24sp"
            android:padding="20dp" />
    
    </LinearLayout>
    

    dialog_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment Dialog Title Text "
            android:padding="10dp"
            android:textColor="#333"
            android:textSize="24sp"/>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="200dp"/>
    
    </LinearLayout>
    

    fragment_dummy_layout.xml

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment Dummy Text"
            android:textSize="24sp"
            android:textColor="#ff0000"/>
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-19 10:20

    The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses:

    AlertDialogA dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout. And also DatePickerDialog or TimePickerDialog.

    These classes define the style and structure for your dialog, but you should use a DialogFragment as a container for your dialog. The DialogFragment class provides all the controls you need to create your dialog and manage its appearance, instead of calling methods on the Dialog object.

    For more detail please go through Dialog Design

    0 讨论(0)
  • 2020-12-19 10:20

    The error sounds like it's looking for the id pager in the xml file for DummyFragment (fragment_dummy_layout.xml). I wonder if this is due to calling View v = inflater.inflate(R.layout.fragment_dummy_layout, container, false); in the same file as setContentView(R.layout.dialog);. Have you tried separating DummyFragment into it's own file?

    You could also try

    View v = getActivity().getLayoutInflater().inflate(R.layout.fragment_dummy_layout, 
            container, false);
    
    0 讨论(0)
提交回复
热议问题