Lukas Rasmus Nielsen
Lukas Rasmus Nielsen

Reputation: 61

Implementation of GridView in android Fragment

Hi I'm fairly new to Android Development and I'm struggling with the use of fragments.

In my fragmentPageAdapter I have 4 cases:

public Fragment getItem(int arg0) {
    // TODO Auto-generated method stub
    switch (arg0) {
        case 0:
            return new friFragment();
        case 1:
            return new freeFragment();
        case 2:
            return new mixedFragment();
        case 3:
            return new recFragment();
        default:
            break;
    }
    return null;
}

I can easliy slide from one fragment to the other, as long as the fragments does NOT contain any code. However, I want to implement a GridView in one of the fragments (recFragment case 3). My first attempt was to initialize the adapter in onCreateView

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

getActivity().setContentView(R.layout.activity_rec);
myGrid = (GridView) getActivity().findViewById(R.id.recGrid);
myGrid.setAdapter(new recAdapter(getActivity().getApplicationContext(), items));
myGrid.setOnItemClickListener(this);

return  inflater.inflate(R.layout.activity_rec,container,false);

This works to the extend that I recieve no errors BUT case 2 is only displayed for half a second after which it jumps to case 3 instead, and at this point it is impossible to slide between fragments.

Using onAttach:

 public void onAttach(Context context) {
    super.onAttach(context);
    this.context = context;
    myGrid = (GridView) getActivity().findViewById(R.id.recGrid);
    myGrid.setAdapter(new recAdapter(getActivity().getApplicationContext(), items));
    myGrid.setOnItemClickListener(this);
}

Yields this Fatal exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference at tryouts.demoapplication.recFragment.onAttach(recipesFragment.java:42) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1019) at android.support.v4.app.BackStackRecord.ensureFragmentsAreInitialized(BackStackRecord.java:1077) at android.support.v4.app.BackStackRecord.beginTransition(BackStackRecord.java:1032) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:658) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) at android.support.v4.view.ViewPager.populate(ViewPager.java:1177) at android.support.v4.view.ViewPager.populate(ViewPager.java:1025) at android.support.v4.view.ViewPager$3.run(ViewPager.java:254) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:603) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

After hours of search of google & stackoverflow I've learned that the problem lies in the context. I thought the solution would be onAttach, but as you can see that did not help me. Unless of course I have misunderstood how to use the onAttach function, which could very well be the case.

So, if I'm correct about the context being the issue, how do I get the correct context?

Or perhaps my approach to implementing the gridview is wrong? If so do you have any suggestions on how I can create a proper implementation of a gridview, in a fragment?

Upvotes: 0

Views: 396

Answers (1)

Ali Taouni
Ali Taouni

Reputation: 71

Maybe this can help you

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

View rootView = inflater.inflate(R.layout.activity_rec,container,false);
// dont forget to initialise the items list

myGrid = (GridView)rootView.findViewById(R.id.recGrid);
myGrid.setAdapter(new recAdapter(rootView.getActivity,items));
myGrid.setOnItemClickListener(this);

return  rootView;

Upvotes: 2

Related Questions