When any sort of view is called outside of the OnCreateView method (or OnActivtivityCreated method for that matter, I have tried both), I get a NullPointerException. To communicate between fragments, I used the interface method which, through my research, seems to be the best way to do it. Unfortunately, I suspect that my error comes from the way that the MainActivity finds the Fragments.
To try to debug what I have so far, I used System.out.println() on the object in OnCreateView()/OnActivityCreated() and there is a memory address meaning it exists and is NOT null, yet when I call it outside of the function from MainActivity, it becomes null...?
Can someone shed some light into my situation? Thanks! (BTW, I have taken some useless parts of the code out)
Tab2Home.java
#Override
public void onActivityCreated (Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
spinner = (Spinner)getActivity().findViewById(R.id.tipSpinner);
adapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(), R.array.tipArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
serviceRate=(RatingBar) getActivity().findViewById(R.id.serviceBar);
//THE OBJECT BELOW IS WHERE THE NULLPOINTEREXCEPTION IS COMING FROM
billAmount = (EditText)getActivity().findViewById(R.id.billEditText);
System.out.println(billAmount); //Surprisingly, this is not null IN the method.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
}
public void transferInfo(){
double bill;
if (String.valueOf(billAmount.getText()).isEmpty()) //billAmount is null OUTSIDE of the method.
bill = 0.00;
else
bill = Double.parseDouble(String.valueOf(billAmount.getText()));
odsl.setData( //odsl is the OnDataSetListener object.
bill,
Integer.parseInt(String.valueOf(spinner.getItemAtPosition(3))),
Integer.parseInt(String.valueOf(numPeopleEdit.getText()))
);
}
public interface OnDataSetListener {
public void setData(double bill, int tip, int ppl);
}
#Override
public void onAttach(Context context){
super.onAttach(context);
try {
odsl = (OnDataSetListener) context;
} catch (Exception e){}
}
And the MainActivity.java
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
mViewPager.setCurrentItem(1);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (position == 2){
Tab2Home homefrag = (Tab2Home) mSectionsPagerAdapter.getItem(1);
homefrag.transferInfo();
System.out.println(".transferInfo() has been called");
}
}
#Override
public void onPageSelected(int position) {
if (position == 3){
Tab2Home homefrag = (Tab2Home) mSectionsPagerAdapter.getItem(2);
homefrag.transferInfo();
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
#Override
public void setData(double bill, int tip, int ppl) {
Tab3Summary summaryfrag = (Tab3Summary) mSectionsPagerAdapter.getItem(2);
summaryfrag.updateInfo(bill, tip, ppl);
}
EDIT:
Here is the error message:
05-23 02:28:00.352 31339-31339/com.uottawa.bond.simpletip E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.uottawa.bond.simpletip, PID: 31339
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.uottawa.bond.simpletip.Tab2Home.transferInfo(Tab2Home.java:97)
at com.uottawa.bond.simpletip.MainActivity$1.onPageScrolled(MainActivity.java:64)
at android.support.v4.view.ViewPager.dispatchOnPageScrolled(ViewPager.java:1954)
at android.support.v4.view.ViewPager.onPageScrolled(ViewPager.java:1928)
at android.support.v4.view.ViewPager.pageScrolled(ViewPager.java:1866)
at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1826)
at android.view.View.updateDisplayListIfDirty(View.java:16037)
at android.view.View.draw(View.java:16834)
at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
at android.support.design.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:1195)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
at android.view.View.draw(View.java:17071)
at android.view.View.updateDisplayListIfDirty(View.java:16050)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728)
at android.view.View.updateDisplayListIfDirty(View.java:16013)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728)
at android.view.View.updateDisplayListIfDirty(View.java:16013)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728)
at android.view.View.updateDisplayListIfDirty(View.java:16013)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728)
at android.view.View.updateDisplayListIfDirty(View.java:16013)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728)
at android.view.View.updateDisplayListIfDirty(View.java:16013)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:656)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:662)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:770)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2796)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2604)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2211)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Find the solution:
1.override the below method
2.Instead of getActivity().findviewbYId use rootView
#Override
public void onViewCreated(View rootView, Bundle savedInstanceState) {
spinner = (Spinner)rootView.findViewById(R.id.tipSpinner);
adapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(), R.array.tipArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
serviceRate=(RatingBar) rootView.findViewById(R.id.serviceBar);
billAmount = (EditText)rootView.findViewById(R.id.billEditText);
System.out.println(billAmount);
}
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 9 months ago.
I am currently trying to create a calendar app in android studio. The app contains a calendar with different "Views" e.g. monthly view, weekly view, and daily view.
The app uses fragments as pages and each view is an activity.
This is the code for the fragment and buttons to initialise each view
public class CalendarFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_calendar, container, false);
Button btn1 = (Button) v.findViewById(R.id.openCalendar);
btn1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
Intent intent = new Intent(getActivity(),CalendarActivity.class);
startActivity(intent);
}
});
Button btn2 = (Button) v.findViewById(R.id.openWeek);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent intent = new Intent(getActivity(), WeekViewActivity.class);
startActivity(intent);
}
});
return v;
}
The first button which displays the monthly view of the calendar called "CalendarActivity" in code works fine but when the second button is clicked which displays the weekly view of the calendar, causes the app to crash and gives the following error in the logcat
2022-05-13 14:44:59.642 15183-15183/com.example.finalyearproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.finalyearproject, PID: 15183
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.finalyearproject/com.example.finalyearproject.WeekViewActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.time.LocalDate.format(java.time.format.DateTimeFormatter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3685)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3842)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7842)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.time.LocalDate.format(java.time.format.DateTimeFormatter)' on a null object reference
at com.example.finalyearproject.CalendarUtils.monthYearFromDate(CalendarUtils.java:16)
at com.example.finalyearproject.WeekViewActivity.setWeekView(WeekViewActivity.java:40)
at com.example.finalyearproject.WeekViewActivity.onCreate(WeekViewActivity.java:29)
at android.app.Activity.performCreate(Activity.java:8054)
at android.app.Activity.performCreate(Activity.java:8034)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1341)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3666)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3842)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7842)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
I am not sure what the problem is as I have been following a tutorial and the other features work. I have also included the code for each activity below.
Monthly View
public class CalendarActivity extends AppCompatActivity implements CalendarAdapter.OnItemListener {
private TextView monthYearText;
private RecyclerView calendarRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);
initWidgets();
CalendarUtils.selectedDate = LocalDate.now();
setMonthView();
}
private void initWidgets()
{
calendarRecyclerView = findViewById(R.id.calendarRecyclerView);
monthYearText = findViewById(R.id.monthYearTV);
}
private void setMonthView()
{
monthYearText.setText(monthYearFromDate(CalendarUtils.selectedDate));
ArrayList<LocalDate> daysInMonth = daysInMonthArray(CalendarUtils.selectedDate);
CalendarAdapter calendarAdapter = new CalendarAdapter(daysInMonth, this);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 7);
calendarRecyclerView.setLayoutManager(layoutManager);
calendarRecyclerView.setAdapter(calendarAdapter);
}
public void nextMonth(View view)
{
CalendarUtils.selectedDate = CalendarUtils.selectedDate.plusMonths(1);
setMonthView();
}
public void previousMonth(View view)
{
CalendarUtils.selectedDate = CalendarUtils.selectedDate.minusMonths(1);
setMonthView();
}
Weekly View
public class WeekViewActivity extends AppCompatActivity implements CalendarAdapter.OnItemListener {
private TextView monthYearText;
private RecyclerView calendarRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_week_view);
initWidgets();
setWeekView();
}
private void initWidgets()
{
calendarRecyclerView = findViewById(R.id.calendarRecyclerView);
monthYearText = findViewById(R.id.monthYearTV);
}
private void setWeekView()
{
monthYearText.setText(monthYearFromDate(CalendarUtils.selectedDate));
ArrayList<LocalDate> days = daysInWeekArray(CalendarUtils.selectedDate);
CalendarAdapter calendarAdapter = new CalendarAdapter(days, this);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 7);
calendarRecyclerView.setLayoutManager(layoutManager);
calendarRecyclerView.setAdapter(calendarAdapter);
}
public void previousWeek(View view)
{
CalendarUtils.selectedDate = CalendarUtils.selectedDate.minusWeeks(1);
setWeekView();
}
public void nextWeek(View view)
{
CalendarUtils.selectedDate = CalendarUtils.selectedDate.plusWeeks(1);
setWeekView();
}
Any suggestions would be greatly appreciated.
My suggestions:
Firstly you didn't initialized CalendarUtils.selectedDate
Cover code with breakpoints for. debugging or with Log.d() messages to find, which variable is null
I have a MainActivity inside I create ViewPager withFragments. I am using savedInstanceState to store values to set TextView after orientation changed. I also want to change value e.g. using button in MainActivity. Button works properly, but if I change the orientation then Button cannot set TextView in Fragment because TextView is now null.
MyFragment
public class MyFragment extends Fragment {
private TextView textView;
private String info;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.item_weather, container, false);
textView = view.findViewById(R.id.text_view);
Bundle bundle = getArguments();
info = bundle.getString("info");
setInfo(info);
return view;
}
public void setInfo(String info) {
this.info = info;
textView.setText(info); //here is the error
}
}
In MainActivity.java in OnCreate
myFragment = new MyFragment();
viewPager = findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(10);
List<Fragment> fragments = new ArrayList<>();
fragments.add(myFragment);
pagerAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
viewPager.setAdapter(pagerAdapter);
MyPagerAdapter looks like this:
public class MyPageAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
}
As I said everything works, but after orientation chage if I use Button I got an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
Any ideas how to solve this?
edit:
full error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app.myapp, PID: 15400
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.app.myapp.fragments.myFragment.setInfo(myFragment.java:53)
at com.example.app.myapp.MainActivity.onOptionsItemSelected(MainActivity.java:195)
at android.app.Activity.onMenuItemSelected(Activity.java:3543)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:407)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:63)
at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:203)
at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:780)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
at android.support.v7.view.menu.MenuPopup.onItemClick(MenuPopup.java:127)
at android.widget.AdapterView.performItemClick(AdapterView.java:318)
at android.widget.AbsListView.performItemClick(AbsListView.java:1159)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3136)
at android.widget.AbsListView$3.run(AbsListView.java:4052)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Into the fragment try to initialize the textView in the on the onViewCreated method:
#Override
public View onViewCreated(view: View, savedInstanceState: Bundle) {
// Your initialization code here
}
Solution:
Ok, EpicPandaForce thanks for hints.
To solve this i used in MainActivity
#Override
protected void onResume() {
super.onResume();
viewPager.setAdapter(pagerAdapter);
}
#Override
protected void onPause() {
super.onPause();
viewPager.setAdapter(null);
}
I get this error when trying to swipe in the ViewPager Activity using the photoView library, I use Glide for the image, and the url are from an Intent(url are array). I also in download data from a firebase database using a model class.
This is the log:
06-27 14:36:33.291 6520-6520/com.realty.drake.kunuk E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.realty.drake.kunuk, PID: 6520
java.lang.StackOverflowError: stack size 8MB
at android.support.v4.view.PagerAdapter.startUpdate(PagerAdapter.java:96)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:662)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:624)
at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:1078)
at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:3089)
at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:284)
at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:45)
at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:34)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1216)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:662)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:624)
at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:1078)
at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:3089)
at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:284)
at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:45)
at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:34)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1216)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:662)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:624)
at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:1078)
at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:3089)
at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:284)
at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:45)
at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:34)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1216)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:662)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:624)
at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:1078)
at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:3089)
at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:284)
at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:45)
at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:34)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1216)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:662)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:624)
at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:1078)
at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:3089)
at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:284)
at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:45)
at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:34)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1216)
at android.support.v4.vie
06-27 14:36:33.411 6520-6520/com.realty.drake.kunuk E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 1275500)
06-27 14:36:33.412 6520-6520/com.realty.drake.kunuk E/AndroidRuntime: Error reporting crash
android.os.TransactionTooLargeException: data parcel size 1275500 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:503)
at android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4447)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:90)
at com.crashlytics.android.core.CrashlyticsUncaughtExceptionHandler.uncaughtExcep tion(CrashlyticsUncaughtExceptionHandler.java:51)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
The ViewPager activity, There I take the url from an intent, parse it to turn it into an array because it is multiple string concatenated. The glide use it to display the image. The first image display and doesn't crash, but when I swipe to the second image, it took a few seconds then crash(only the activity crash not the app), but If I swipe from second to the third image it crash suddenly before even display it.
public class ViewPagerActivity extends AppCompatActivity {
String imageList = null;
String[] propertyImageArray = {};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
//get imageList url from Intent
imageList = getIntent().getStringExtra("imageList");
propertyImageArray = imageList.split(",");
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(new SamplePagerAdapter());
}
class SamplePagerAdapter extends PagerAdapter {
#Override
public int getCount() {
return propertyImageArray.length;
}
#Override
public View instantiateItem(ViewGroup container, int position) {
//Must call notifyDataSetChanged before adding imageList to View
//otherwise it'll crash
notifyDataSetChanged();
PhotoView photoView = new PhotoView(ViewPagerActivity.this);
GlideApp.with(ViewPagerActivity.this)
.load(propertyImageArray[position])
.fitCenter()
.into(photoView);
// Now just add PhotoView to ViewPager and return it
container.addView(photoView,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return photoView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
This is the class that send the intent to ViewPagerActivity,
public class PropertyLotDetail extends AppCompatActivity implements Toolbar.OnMenuItemClickListener {
#Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.property_lot_detail);
Toolbar myToolbar = (Toolbar) findViewById(R.id.kunuk_toolbar);
myToolbar.inflateMenu(R.menu.menu_detail);
setSupportActionBar(myToolbar);
myToolbar.setOnMenuItemClickListener(this);
//collect our intent
Intent intent = getIntent();
Property property = intent.getParcelableExtra("Property");
//collect all property values from Parcelable
int price = property.getPrice();
String address = property.getAddress();
final String propertyImage = property.getPropertyImage();
float lotDim = property.getLotDim();
String propertyDesc = property.getPropertyDesc();
String lotTerrainType = property.getLotTerrainType();
String lotTerrainDimExt = property.getLotTerrainDimExt();
String lotTerrainSteepness = property.getLotTerrainSteepness();
[...]
//Bind the data from the Parcelable to the Views
TextView addressDetail = findViewById(R.id.post_address);
addressDetail.setText(String.valueOf(address));
String currencyPrice = NumberFormat //Format the price variable in currency form
.getCurrencyInstance(Locale.US)
.format(price);
TextView priceDetail = findViewById(R.id.post_price);
priceDetail.setText(currencyPrice);
ImageView imageView = findViewById(R.id.post_propertyImage);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),
ViewPagerActivity.class)
.putExtra("imageList", propertyImage)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
});
//take one long string containing multiple url and parse it
String propertyImageArray[] = propertyImage.split(",");
//TODO add loading icon for placeholder
// Download directly from StorageReference using Glide
// (See MyAppGlideModule for Loader registration)
GlideApp.with(getApplication())
.load(propertyImageArray[0])
.fitCenter()
.into(imageView);
That's the most relevant information. Please help
I just got rid of the notifyDataChanged() method call. I presume it has been automatically called, I didn't need to do it, and that's why the stack trace leads to an endless recursion. It's just what I think.
public class ViewPagerActivity extends AppCompatActivity {
String imageList = null;
String[] propertyImageArray = {};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
//get imageList url from Intent
imageList = getIntent().getStringExtra("imageList");
propertyImageArray = imageList.split(",");
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(new SamplePagerAdapter());
}
class SamplePagerAdapter extends PagerAdapter {
#Override
public int getCount() {
return propertyImageArray.length;
}
#Override
public View instantiateItem(ViewGroup container, int position) {
//Must call notifyDataSetChanged before adding imageList to View
//otherwise it'll crash
notifyDataSetChanged();
PhotoView photoView = new PhotoView(ViewPagerActivity.this);
GlideApp.with(ViewPagerActivity.this)
.load(propertyImageArray[position])
.fitCenter()
.into(photoView);
// Now just add PhotoView to ViewPager and return it
container.addView(photoView,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return photoView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
Sorry for the bad formatting, don't know how to make striketrough in code
Issue
First time it works fine. this app download image from the internet and save into external storage.
I want to make a method when i call the method then it will repopulate the image from external storage and set new adapter to show the all images on PagerView.
In simple words i want to refresh the pagerview screens view. on app
start there is 3 images in sdcard. and when i press button then there
is 4 images in sdcard. it should update/refresh the view.
This the custom adapter class
public class CustomSwipeAdapter extends PagerAdapter {
static ImageView i1;
String pdfName;
static Bitmap[] bmp ;
private Context ctx;
private LayoutInflater layoutInflater;
public CustomSwipeAdapter(Context ctx) {
this.ctx =ctx;
}
#Override
public int getCount() {
bmp=Picgraber();
return bmp.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view==(LinearLayout)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
//Picgraber();
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout,container,false);
i1 = (ImageView)item_view.findViewById(R.id.swipeimageview);
TextView t1 = (TextView)item_view.findViewById(R.id.swipetextview);
// i1.setImageResource(images[position]);
i1.setImageBitmap(bmp[position]);
i1.setTag(imagesstr[position]);
t1.setText("image "+ position);
pdfName = String.valueOf(i1.getResources());
Log.d("pdfname", pdfName);
Log.d("position", String.valueOf(position));
container.addView(item_view);
return item_view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
public Bitmap[] Picgraber(){
ArrayList<Bitmap> list = new ArrayList<Bitmap>();
File file= new File("/sdcard/com.shah.tabs/");
File[] folders=file.listFiles();
for(File ff:folders){
File it=new File(file.getPath()+File.separator+ff.getName()+File.separator+"image"+File.separator+ff.getName()+".png");
// Log.w("folder with path",it.getPath());
list.add(BitmapFactory.decodeFile(it.getPath()));
}
Bitmap[] Array = list.toArray(new Bitmap[list.size()]);
return Array;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
public void update(){
bmp = Picgraber();
this.notifyDataSetChanged();
}
}
This is the onCreate method of the activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_books);
packageFolder = new File(Environment.getExternalStorageDirectory() + File.separator + getApplicationContext().getPackageName());
if (!packageFolder.exists()) {
packageFolder.mkdir();
}
viewPager = (ViewPager) findViewById(R.id.viewpager);
adapter = new CustomSwipeAdapter(this);
viewPager.setAdapter(adapter);
Log.d("curritem", String.valueOf(viewPager.getCurrentItem()));
viewPager.setPageTransformer(true, new ZoomOutSlideTransformer());
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
pos = position;
Log.d("onpagechange", String.valueOf(pos));
Log.d("imagename", String.valueOf(CustomSwipeAdapter.i1.getTag()));
}
});
viewPager.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
x1 = event.getX();
y1 = event.getY();
break;
}
case MotionEvent.ACTION_UP: {
x2 = event.getX();
y2 = event.getY();
if (x1 < x2) {
}
if (x1 > x2) {
}
if (y1 < y2) {
}
if (y1 > y2 + 100) {
downtoupswipe();
}
break;
}
}
return false;
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, 1);
}
});
}
This the logcat of the application when crash
10-10 11:00:50.388 16816-16816/com.shah.tabs E/InputEventReceiver: Exception dispatching input event.
10-10 11:00:50.389 16816-16816/com.shah.tabs E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
10-10 11:00:50.390 16816-16816/com.shah.tabs E/MessageQueue-JNI: java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 2, found: 3 Pager id: com.shah.tabs:id/viewpager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.shah.tabs.CustomSwipeAdapter
at android.support.v4.view.ViewPager.populate(ViewPager.java:1071)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2114)
at android.view.View.dispatchTouchEvent(View.java:8471)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2399)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2092)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2369)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1719)
at android.app.Activity.dispatchTouchEvent(Activity.java:2742)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2330)
at android.view.View.dispatchPointerEvent(View.java:8666)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4123)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3989)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5807)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5781)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5752)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5897)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:122)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
10-10 11:00:50.390 16816-16816/com.shah.tabs D/AndroidRuntime: Shutting down VM
10-10 11:00:50.393 16816-16816/com.shah.tabs E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.shah.tabs, PID: 16816
java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 2, found: 3 Pager id: com.shah.tabs:id/viewpager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.shah.tabs.CustomSwipeAdapter
at android.support.v4.view.ViewPager.populate(ViewPager.java:1071)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2114)
at android.view.View.dispatchTouchEvent(View.java:8471)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2399)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2092)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2369)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1719)
at android.app.Activity.dispatchTouchEvent(Activity.java:2742)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2330)
at android.view.View.dispatchPointerEvent(View.java:8666)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4123)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3989)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5807)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5781)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5752)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5897)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:122)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
**
Call from activity fab button
**
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
adapter.update();
}
});
Using the array of bitmaps have a performance issues. You can use any ImageLoader libs to do display image.
The Universal ImageLoader has methods to display local images
make some changes in this class
CustomSwipeAdapter
#Override
public int getCount() {
return imagesstr.length();
}
//add this method
public void setImages(String image[]){
imagesstr = image;
this.notifyDataSetChanged();
}
to display image use the imageLoader as suggested earlier
Just take a look at the Exception message you got here. It says:
java.lang.IllegalStateException: The application's PagerAdapter
changed the adapter's contents without calling
PagerAdapter#notifyDataSetChanged! Expected adapter item count: 2,
found: 3
It is pretty clear from the Exception message that while refreshing your PagerView, you are adding new images but you are not telling the adapter that the data which it is going to populate has been changed. As a result, the adapter expecting to just populate 2 images find 3 images and your application crashes.
So whenever you add a new image you need to explicitly call adapter.notifyDataSetChanged() to tell the adpater that a new image has been added which would eventually solve this issue.
I am using a ViewPager fragment which has two fragments as children. This works great, however when I replace the ViewPager fragment by another fragment and replace this fragment by the ViewPager fragment my application crashes with the following NullPointerException:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:667)
at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:211)
at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1319)
at android.view.View.dispatchRestoreInstanceState(View.java:13756)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2888)
at android.view.View.restoreHierarchyState(View.java:13734)
at android.support.v4.app.Fragment.restoreViewState(Fragment.java:468)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1094)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
My ViewPager fragment consists of the following source code instanciating the two child fragments in case they are null. Furthermore, the ViewPagerAdapter implementation is instanciated and assigned to the ViewPager.
public class ConnectionPasswordViewPagerFragment extends Fragment
{
private ViewPager vpConnectionPassword;
private ConnectionPasswordViewPagerAdapter paConnectionPassword;
private ConnectionPasswordGeneratorMACAddress passwordGeneratorMACAddress;
private ConnectionPasswordGeneratorSerialNumber passwordGeneratorSerialNumber;
public ConnectionPasswordViewPagerFragment()
{
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if ((this.passwordGeneratorSerialNumber == null) && (this.passwordGeneratorMACAddress == null))
{
this.passwordGeneratorMACAddress = new ConnectionPasswordGeneratorMACAddress();
this.passwordGeneratorSerialNumber = new ConnectionPasswordGeneratorSerialNumber();
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_connection_password_view_pager, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
/*if (this.paConnectionPassword == null)
{*/
this.paConnectionPassword = new ConnectionPasswordViewPagerAdapter(this.getChildFragmentManager(), this.getActivity(), this.passwordGeneratorMACAddress, this.passwordGeneratorSerialNumber);
//}
this.vpConnectionPassword = (ViewPager) this.getActivity().findViewById(R.id.vpConnectionPassword);
this.vpConnectionPassword.setAdapter(this.paConnectionPassword);
}
}
The ViewPagerAdapter shown below implementation keeps the reference to the child fragments in an ArrayList and implements the metods getItem(), getCount() as well as getPageTitle(). The class ViewPagerFragment simply extends the support Fragment class and provides the abstract method getPageTitleStringID() implemented by the child fragments.
public class ConnectionPasswordViewPagerAdapter extends FragmentStatePagerAdapter
{
private static final byte NUMBER_OF_FRAGMENTS = (byte) 2;
private ArrayList<ViewPagerFragment> childFragments;
private Activity displayActivity;
public ConnectionPasswordViewPagerAdapter(FragmentManager fragmentMgm, Activity displayAct, ConnectionPasswordGeneratorMACAddress generatorMACAddress, ConnectionPasswordGeneratorSerialNumber generatorSerialNumber)
{
super(fragmentMgm);
this.childFragments = new ArrayList<>(ConnectionPasswordViewPagerAdapter.NUMBER_OF_FRAGMENTS);
this.setDisplayActivity(displayAct);
if (generatorMACAddress != null)
{
this.childFragments.add(generatorMACAddress);
}
if (generatorSerialNumber != null)
{
this.childFragments.add(generatorSerialNumber);
}
}
public Activity getDisplayActivity()
{
return this.displayActivity;
}
public void setDisplayActivity(Activity value)
{
this.displayActivity = value;
}
#Override
public Fragment getItem(int position)
{
return this.childFragments.get(position);
}
#Override
public int getCount()
{
return ConnectionPasswordViewPagerAdapter.NUMBER_OF_FRAGMENTS;
}
#Override
public CharSequence getPageTitle(int position)
{
return this.getDisplayActivity().getString(this.childFragments.get(position).getPageTitleStringID());
}
}
Thanks for your help!
I fixed the issue by simply creating the fragment containing the view pager every time I call the support fragment manager's replace method.
public void onSwitchToConnectionPasswordGenerator(View clickedView)
{
this.fragConnectionPassword = new ConnectionPasswordViewPagerFragment();
this.getSupportFragmentManager().beginTransaction().replace(R.id.flFragmentContainer, this.fragConnectionPassword).commitAllowingStateLoss();
}
I think your Viewpager is in Fragment, and this fragment have setRetainInstance set to true (i have this error too when i try to retain the parent fragment)
Better way is to you can check for FragmentManager to get Fragment if its there, if not then create a new instance as follows:
FragmentManager manager = getSupportFragmentManager();
Fragment fragment = manager.findFragmentByTag("YourFragmentName");
if(fragment == null)
fragment = new YourFragment();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.replace(Your_Fragmnet_Container_Layout_id, fragment, fragment.getClass().getSimpleName());
fragmentTransaction.commit;