0

I have custom dialog to show which has instruction about how to use the app.I am using ViewPager for this inside my custom dialog layout. I am getting error java.lang.IllegalArgumentException: No view found for id for fragment FragmentForInstruction1.

I have created a method which is called in onCreate(Bundle savedInstanceState) {} method .The method inflates the layout for dialog.

private void showCustomDialogForInstruction() {
        final Dialog dialog = new Dialog(MainActivity.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View dialogLayout = inflater.inflate(R.layout.dialog_layout_for_instruction_message, null, false);
        layout.setAlpha(0.2f);

        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(dialogLayout);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));


        ViewPager viewPager=(ViewPager) dialog.findViewById(R.id.pagerInstruction);
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        adapter.addFrag(new FragmentForInstruction1());
        adapter.addFrag(new Fragme`enter code here`ntForWelcomePage2());
        adapter.addFrag(new FragmentForWelcomePage3());
        adapter.addFrag(new FragmentForWelcomePage4());
        viewPager.setAdapter(adapter);



        viewPager.addOnPageChangeListener(new OnPageChangeListener()
        {

            @Override
            public void onPageSelected(int pos)
            {


            }

            @Override
            public void onPageScrolled(int pos, float arg1, int arg2)
            {

            }

            @Override
            public void onPageScrollStateChanged(int arg0)
            {

            }
        });


        Button done = (Button) dialog.findViewById(R.id.done);
        done.setText("Got It");
        done.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        Button neverShow = (Button) dialog.findViewById(R.id.nevershow);
        neverShow.setText("Never Show Again");
        neverShow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                    KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        dialog.getWindow().setBackgroundDrawable(
                new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.show();
    }

my view pager class:

public class ViewPagerAdapter extends FragmentPagerAdapter 
{
     private final List<Fragment> mFragmentList = new ArrayList<Fragment>();

     public ViewPagerAdapter(FragmentManager manager)
     {
         super(manager);
     }

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

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

     public void addFrag(Fragment fragment)
     {
         mFragmentList.add(fragment);
     }


}

and i call the method in MainActivity :

public class MainActivity extends AppCompatActivity  {

    SharedPreferences sharedPreferencesNeverShowAgain ;
    boolean neverShowAgain;


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


        sharedPreferencesNeverShowAgain = PreferenceManager.getDefaultSharedPreferences(this);
        neverShowAgain = sharedPreferencesNeverShowAgain.getBoolean("NeverShowAgain", false);
        if(!neverShowAgain){
            showCustomDialogForMessage();
        }


    }
}

and the layout for dialog is:

<?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="wrap_content"
    android:background="@drawable/dialog_shape"
    android:orientation="vertical" >

    <!-- layout title -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#00000000"
        android:gravity="center"
        android:orientation="vertical" >

        <ViewFlipper
            android:id="@+id/flipper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/top_edge_rounded"
            android:flipInterval="2000"
            android:padding="20dp" >

            <TextView
                android:id="@+id/dialog_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:gravity="center"
                android:text="Input Height"
                android:textColor="@color/textColor"
                android:textStyle="normal" />

            <TextView
                android:id="@+id/title1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:gravity="center"
                android:text="Input Height"
                android:textColor="@color/accent"
                android:textStyle="normal" />
        </ViewFlipper>

        <View
            android:id="@+id/tri"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/triangle"
            android:rotation="180" />
    </LinearLayout>

    <!-- layout dialog content -->

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="300dp" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <android.support.v4.view.ViewPager
                android:id="@+id/pagerInstruction"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:id="@+id/layoutIndicater"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:orientation="horizontal" >
            </LinearLayout>
        </RelativeLayout>
    </ScrollView>
    <!-- layout dialog buttons -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_margin="10dp"
        android:background="@drawable/all_rounded_edge_plum_for_dialog" >

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_alignParentTop="true"
            android:background="@color/textColor" />

        <View
            android:id="@+id/ViewColorPickerHelper"
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="@color/textColor" />

        <Button
            android:id="@+id/nevershow"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/ViewColorPickerHelper"
            android:background="?android:attr/selectableItemBackground"
            android:padding="5dp"
            android:text="Close"
            android:textColor="@color/textColor" />

        <Button
            android:id="@+id/done"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/ViewColorPickerHelper"
            android:background="?android:attr/selectableItemBackground"
            android:padding="5dp"
            android:text="Done"
            android:textColor="@color/textColor" />
    </RelativeLayout>

</LinearLayout>

FragmentForInstruction1 code:

public class FragmentForInstruction1 extends Fragment{

    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragmentinstructionpage1, container,false);
        return view;
    }


}

and its layout is:

<?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:orientation="vertical"
    android:background="@color/backgroundColor"
    android:gravity="center" >


     <TextView 
        android:id="@+id/titleWelcomeFragment1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@color/colorPrimary"
        android:textSize="30sp"
        android:textStyle="bold"/>  

    <TextView 
        android:id="@+id/messageWelcomeFragment1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@color/colorPrimary"
        android:textSize="20sp"
        android:textStyle="normal"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"/>


</LinearLayout>
Mayank
  • 181
  • 2
  • 9

1 Answers1

0

You should create an adapter that extends FragmentPagerAdapter:

private class CustomPagerAdapter extends FragmentPagerAdapter {

    public CustomPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new FragmentForInstruction1();
            case 1:
                return new FragmentForInstruction2();
            case 2:
                return new FragmentForInstruction3();
            case 3:
                return new FragmentForInstruction4();
            default:
                return new FragmentForInstruction1();
        }
    }

    @Override
    public int getCount() {
        return 4;
    }
}

While crating an instance of it, pass the getChildFragmentManager() instead of getSupportFragmentManager():

viewPager.setAdapter(new CustomPagerAdapter(getChildFragmentManager()));
Shayan_Aryan
  • 2,002
  • 1
  • 29
  • 31
  • It show error :The method getChildFragmentManager() is undefined for the type MainActivity. My MainActivity extends AppCompatActivity.Min api version is 14 – Mayank Nov 26 '15 at 06:29
  • are you creating this class inside MainActivity or in DialogFragement? – Shayan_Aryan Nov 26 '15 at 06:46
  • I have main activity in which i call the method for dialog,viewPagerAdapter is another class for view pager.I have notused DialogFragment.If i inflate the layout without viewpager and fragment ,it works and displayed on screen,but if add viewPager.setAdapter(new CustomPagerAdapter(getSupportFragmentManager())); then it shows error. – Mayank Nov 26 '15 at 07:03
  • You should use DialogFragment in order to show a dialog containing other fragments (fragments within viewPager) – Shayan_Aryan Nov 26 '15 at 09:13
  • I have created a new class AlertDFragment extends DialogFragment,it does not show error but it does not display the fragment data,i got the blank screen in dialog. – Mayank Nov 26 '15 at 09:24
  • onCreateView() :I inflate the layout. onViewCreated(View view, @Nullable Bundle savedInstanceState):I have written the pager code and other layout element behavior. – Mayank Nov 26 '15 at 09:31
  • final LinearLayout indicaterLayout=(LinearLayout)view. findViewById(R.id.layoutIndicater); ViewPager viewPager=(ViewPager) view.findViewById(R.id.pagerInstruction); final ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager()); adapter.addFrag(new FragmentForWelcomePage1()); adapter.addFrag(new FragmentForWelcomePage2()); adapter.addFrag(new FragmentForWelcomePage3()); adapter.addFrag(new FragmentForWelcomePage4()); viewPager.setAdapter(adapter); – Mayank Nov 26 '15 at 09:31
  • try to use the CustomPagerAdapter I wrote inside onViewCreated() method – Shayan_Aryan Nov 26 '15 at 09:50
  • actually the problem was scrollview in layout ,I have removed it and its working fine.Thanks for your help. – Mayank Nov 26 '15 at 10:47