0

Trying to implement changing an image within a class extending fragment. After clicking the button in the menu from MainActivity with FragmentPagerAdapter app always crashes.

At first, I got a null error from getActivity() within the fragment, then replaced these with sending ImageView from main activity button click. It helped for a moment then I had to move int array declaration from onCreateView() or else it was empty again, yet a final bad boy I had to face was super(context).

According to Google and stuff that happens due to the creation of activity comes after onCreateView() hence getActivity() and context and public variables are unreachable. How to replace it? Where should I put some special line to get context from the fragment and get access to everything inside?

Here is my code

//from main activity click
private void loadImage() {
    ImageView v = findViewById(R.id.view_core);
    FragmentHistogram frag = new FragmentHistogram();
    getFragmentManager().executePendingTransactions(); //that str also did not help
    frag.reseter(v);
}

//tried that in fragment but that did not help
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof Activity){
        a=(Activity) context;
    }
}

===UPDATE===

// in MainActivity
TabAdapter adapter;
Fragment fr;
@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_tabs);

    ViewPager viewPager = findViewById(R.id.view_pager);
    TabLayout tabLayout = findViewById(R.id.tabs);

    adapter = new TabAdapter(this, getSupportFragmentManager()); //FragmentPagerAdapter 
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
    fr = adapter.getItem(1); 
} 

 // modified on click also in MainActivity
   private void loadImage() {
    ((FragmentHistogram) fr).reseter(v); //FragmentHistogram fragment class with reseter() method
} 

But that also does not work, context is again null. In onCreate I already have passed context this into adapter hence context is already not null, then I define exact fragment from its position and get method via casting. However, if even in that case I remove v from reseter() then the view cannot be found as it activity has not started.

One more thing I forgot to mention. I have an inner class within fragment class it required context. Maybe that is the problem?

class lHistogram extends View {

    public lHistogram(Context context, Bitmap bi) {            
        super(context);
    }

===UPDATE 2===

public class TabAdapter extends FragmentPagerAdapter {

final int PAGE_COUNT = 2;
private Context mContext;

public TabAdapter(Context context, FragmentManager fm) {
    super(fm);
    mContext = context;
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new FragmentCore();
        default:
            return new FragmentHistogram();
    }
...
}

I guess that problem might be in fragment creation order. What I mean is having added to onViewCreated log lines I found out that no matter which order I set in Fragment getItem(int position) it always sends context to FragmentCore, which means even having FragmentHistogram showing as 1st page context is still on the second. What controls context assigning?

JayJayAbrams
  • 195
  • 1
  • 16
  • 1
    `FragmentHistogram frag = new FragmentHistogram();` will give you a new fragment not the current one . – ADM Jul 08 '18 at 17:47
  • @ADM well now that seems straightforwand, thank you! Any ideas how then can I access both method from class and class content altogether? – JayJayAbrams Jul 08 '18 at 17:52
  • https://stackoverflow.com/questions/9294603/get-currently-displayed-fragment – ADM Jul 09 '18 at 03:39
  • Did you add fragment into your activity? a random thought. maybe a complete code would helps – hjchin Jul 10 '18 at 05:07
  • @hjchin I have updated the post, fragments are added inside of TabAdapter class, but I see now that problem determine what controls how and why context is assigned only to FragmentCore, and possibly after I could get context from it. – JayJayAbrams Jul 10 '18 at 08:18
  • i see.. u should not get the fragment from adapter, it always return a new fragment. U should be able to get the fragment from SupportFragmentManager, like method https://developer.android.com/reference/android/support/v4/app/FragmentManager.html#getFragments(). – hjchin Jul 10 '18 at 09:28

1 Answers1

0

Solutions, pick 1 or 2; both worked for my case:

  1. https://stackoverflow.com/a/51253168/8859693

Add to each Fragment's onCreate(), try to call the method setRetainInstance(true);

  1. https://stackoverflow.com/a/51271379/8859693

Add mcontext=getContext(); class extention of FragmentPagerAdapter

public class TabAdapter extends FragmentPagerAdapter {

final int PAGE_COUNT = 2;
private Context mContext;

public TabAdapter(Context context, FragmentManager fm) {
    super(fm);
    mContext = context;
}
JayJayAbrams
  • 195
  • 1
  • 16