-2

I have an Activity say MyActivity, that has a ViewPager who sets a FragmentStatePagerAdapter which adapts a given amount of fragments. In addition I want to add a CirclePageIndicator (Jake wharton's) on all the fragments.

My problem is: my ViewPager is in MyActivity.xml file, but my CirclePageIndicator view, is in the fragment.xml file. In order to trigger the CirclePageIndicator I need to use a setViewPager method, which requires as a parameter a ViewPager object, which means I need to trigger the CirclePageIndicator in MyActivity class where I initialize the ViewPager obj.

How can I safely call the CirclePageIndicator In MyActivity despite it being in the fragment.xml file.

Important to notice, I've came along a supposedly similar question where they suggested to access the fragment like this:

pageFragment = (PageFragment) getSupportFragmentManager().findFragmentById(R.id.fragId);
pageFragment.circleIndicator.setViewPager(viewPager);

but I still recieved a nullPointerException while attempting access the circleIndicator!! as following:

Caused by: java.lang.NullPointerException: Attempt to read from field 'com.viewpagerindicator.CirclePageIndicator com.example.ibm.shoppingequalizer.appintrolib.PageFragment.circleIndicator' on a null object reference
                                                                                   at com.example.ibm.shoppingequalizer.appintrolib.MyActivity.onCreate(MyActivity.java:32)

Here is my code:

MyActivity.java

public class AppIntro extends FragmentActivity {

ViewPager viewPager;
PageFragment pageFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_intro2);
    viewPager = (ViewPager) findViewById(R.id.viewPager);

    PageFragment.SwipeAdapter swipeAdapter = new PageFragment.SwipeAdapter(getSupportFragmentManager());
    viewPager.setAdapter(swipeAdapter);


    //In order to use CirclePageIndicator I need 
    //something like this ->  circleIndicator.setViewPager(viewPager);

 }
}  

my_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ibm.shoppingequalizer.appintrolib.MyActivity">

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</RelativeLayout>

MyFragment.java

public class PageFragment extends Fragment {

public static PageFragment newInstance(String title, String msg, int thumb) {
    PageFragment fragment = new PageFragment();

    Bundle args = new Bundle();
    args.putString("title", title);
    args.putString("msg", msg);
    args.putInt("thumb", thumb);
    fragment.setArguments(args);

    return fragment;
}

TextView message;
TextView title;
ImageView imageView;
CirclePageIndicator circleIndicator;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_page, container, false);

    String title1 = getArguments().getString("title");
    String msg = getArguments().getString("msg");


    message = (TextView) view.findViewById(R.id.msg);
    title = (TextView) view.findViewById(R.id.title);
    imageView = (ImageView) view.findViewById(R.id.imageView);
    circleIndicator = (CirclePageIndicator) view.findViewById(R.id.circleIndicator);

    setTextFonts();

    title.setText(title1);
    message.setText(msg);



    return view;
   }
}

SwipeAdapter.java

static class SwipeAdapter extends FragmentStatePagerAdapter {

    List<IntroPageInfo> allInfo = getItem();

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

    public List<IntroPageInfo> getItem() {
        List<IntroPageInfo> allInfo = new ArrayList<>();

        allInfo.add(new IntroPageInfo("A",
                "blalalala",
                R.drawable.grocery_shopping_with_the_holy_spirit));
        allInfo.add(new IntroPageInfo("B",
                "didndiddidind",
                R.drawable.category_intro));
        allInfo.add(new IntroPageInfo("C",
                "ciccicicicici",
                R.drawable.category_intro));
        allInfo.add(new IntroPageInfo("D",
                "ding dang dong",
                R.drawable.category_intro));
        allInfo.add(new IntroPageInfo("E",
                "ehmmmmmmmmmmmmmmm",
                R.drawable.category_intro));

        return allInfo;
    }


    @Override
    public Fragment getItem(int i) {
        IntroPageInfo info = allInfo.get(i);
        PageFragment fragment = new PageFragment().newInstance(info.title, info.message, info.image);
        return fragment;
    }

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

IntroPageInfo.java

static class IntroPageInfo {
    String title;
    String message;
    int image;

    public IntroPageInfo(String title, String message, int image) {
        this.title = title;
        this.message = message;
        this.image = image;
    }
}
Hudi Ilfeld
  • 1,905
  • 2
  • 16
  • 25
  • 1
    Possible duplicate of [Call an activity method from a fragment](https://stackoverflow.com/questions/12659747/call-an-activity-method-from-a-fragment) – Aks4125 Nov 28 '17 at 10:19

2 Answers2

0

in case you want to call a view on Activity from Fragment,

try implement this in activity, to provide the way to call your view from this activity through interface

public class TestActivity implement GetTextCallback {
   public interface GetTextCallback {
       String getTextYouWant()
   }

   @Override
   public String getTextYouWant() {
       return editText.getText().toString();
   }
}

and this in fragment, to call a view from your activity through interface you created

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (getTextCallback == null) {
        getTextCallback = (GetTextCallback) activity;
    }
}

public void whenYouWantToGetText() {
    if (getTextCallback != null) {
        getTextCallback.getTextYouWant();
    } 
}
JiratPasuksmit
  • 672
  • 7
  • 18
0

You should put CirclePageIndicator in your activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_intro2);
    viewPager = (ViewPager) findViewById(R.id.viewPager);

    PageFragment.SwipeAdapter swipeAdapter = new PageFragment.SwipeAdapter(getSupportFragmentManager());
    viewPager.setAdapter(swipeAdapter);
    circleIndicator = (CirclePageIndicator) findViewById(R.id.circleIndicator);
    circleIndicator.setupWithViewPager(viewPager);


 }

Modify xml file accordingly too. Move circleIndicator from fragment to activity xml file. The activity.xml skeleton should be something like this:

--> Toolbar 
--> Viewpager 
--> CircleIndicator
tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
  • What do u mean to modify xml file? what exactly? The circleIndicator? I cannot do so. since the Activity xml file is a viewPager? or did u mean something else? – Hudi Ilfeld Nov 28 '17 at 10:09
  • Yes, that's why you should move your `R.id.circleIndicator` to activity from activity. Check my updated answer. – tahsinRupam Nov 28 '17 at 10:10
  • but my Viewpager is suppose to cover the hole Screen being match_parent on both height and width!! since the circleIndicator is on the fragment! not below!! – Hudi Ilfeld Nov 28 '17 at 10:22
  • Where do you want to show your `circleIndicator` exactly? Can you provide any image/sketch please?? – tahsinRupam Nov 28 '17 at 10:25
  • I don'd know exactly how to provide image/skecth on stackoverflow. but what I mean is pretty simple. the Activity has a viewPage that sets the fragments. and **on** the background of the fragments towrds the bottom of the fragment I want a circleIndicator. I cannot put. below the viewPager since I need. the indicator on the fragments!!!! :) – Hudi Ilfeld Nov 28 '17 at 10:42
  • Why would you need your `circlrIndicator` in fragment? If you have 10 fragments then would you like to have 10 different `circleIndicator`s? This would be totally wrong! If you want to put full height and width to `viewPager`, you can use `relativeLayout` and put the indicator **above** your `viewPager`. Hope that makes sense. – tahsinRupam Nov 28 '17 at 11:10
  • thnx so much man. you were right. I totally forgot about the relative layout floating feature. you were a big help. :) – Hudi Ilfeld Nov 28 '17 at 12:41
  • My pleasure. :) – tahsinRupam Nov 29 '17 at 12:15