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