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?