0
votes

In my app i want to use a gridView in fragment of navigation drawer but my application is getting crash. this is my code:

public class HomeFragment extends Fragment {

    GridView gridView;
    private String[] categoryHomeGridView;
    private Integer[] icon={R.drawable.cat_offer_women,R.drawable.cat_offer_men,
            R.drawable.cat_offer_food_and_drink,R.drawable.cat_offer_electronics};
    private ArrayList homeGridViewItems;
    private HomeGridViewListAdapter adapter;
    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        init();
        return view;
    }

    private void init() {
        // TODO Auto-generated method stub
        gridView=(GridView) getActivity().findViewById(R.id.homeGridView);
        categoryHomeGridView=getActivity().getResources().getStringArray(R.array.category_array);


        homeGridViewItems=new ArrayList();
        homeGridViewItems.add(new HomeGridViewItem(categoryHomeGridView[0],
                icon[0]));
        homeGridViewItems.add(new HomeGridViewItem(categoryHomeGridView[1],
                icon[1]));
        homeGridViewItems.add(new HomeGridViewItem(categoryHomeGridView[2],
                icon[2]));
        homeGridViewItems.add(new HomeGridViewItem(categoryHomeGridView[3],
                icon[3]));
    //  iconHomeGridView.recycle(); 

        adapter=new HomeGridViewListAdapter(getActivity().getApplicationContext()
                ,homeGridViewItems);
        gridView.setAdapter(adapter); 

    }
}

and this is crash log

03-26 14:50:56.524: E/AndroidRuntime(2355): FATAL EXCEPTION: main
03-26 14:50:56.524: E/AndroidRuntime(2355): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.couponoffer/com.example.couponoffer.MainActivity}: java.lang.NullPointerException
03-26 14:50:56.524: E/AndroidRuntime(2355):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at android.os.Looper.loop(Looper.java:137)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at java.lang.reflect.Method.invokeNative(Native Method)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at java.lang.reflect.Method.invoke(Method.java:511)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at dalvik.system.NativeStart.main(Native Method)
03-26 14:50:56.524: E/AndroidRuntime(2355): Caused by: java.lang.NullPointerException
03-26 14:50:56.524: E/AndroidRuntime(2355):     at fragments.HomeFragment.init(HomeFragment.java:57)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at fragments.HomeFragment.onCreateView(HomeFragment.java:34)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at android.app.BackStackRecord.run(BackStackRecord.java:635)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at android.app.Activity.performStart(Activity.java:5017)
03-26 14:50:56.524: E/AndroidRuntime(2355):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
03-26 14:50:56.524: E/AndroidRuntime(2355):     ... 11 more
3

3 Answers

0
votes

Place this line gridView=(GridView) getActivity().findViewById(R.id.homeGridView); in onCreate and do it like this gridView=(GridView) view.findViewById(R.id.homeGridView); because your gridview is part of your View. Or pass the View view to init();

like this:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        init(view);
        return view;
    }

private void init(View rootView) {
        // TODO Auto-generated method stub
        gridView=(GridView) rootView.findViewById(R.id.homeGridView);
        categoryHomeGridView=getActivity().getResources().getStringArray(R.array.category_array);


        homeGridViewItems=new ArrayList();
        homeGridViewItems.add(new HomeGridViewItem(categoryHomeGridView[0],
                icon[0]));
        homeGridViewItems.add(new HomeGridViewItem(categoryHomeGridView[1],
                icon[1]));
        homeGridViewItems.add(new HomeGridViewItem(categoryHomeGridView[2],
                icon[2]));
        homeGridViewItems.add(new HomeGridViewItem(categoryHomeGridView[3],
                icon[3]));
    //  iconHomeGridView.recycle(); 

        adapter=new HomeGridViewListAdapter(getActivity().getApplicationContext()
                ,homeGridViewItems);
        gridView.setAdapter(adapter); 

    }
0
votes

The gridView probably belongs to the fragment layout

So you need to change this

 gridView=(GridView) getActivity().findViewById(R.id.homeGridView);

to

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
 View view = inflater.inflate(R.layout.fragment_home, container, false)
 gridView=(GridView) view.findViewById(R.id.homeGridView);
0
votes

Change this:

gridView=(GridView) getActivity().findViewById(R.id.homeGridView);

to this:

View view = inflater.inflate(R.layout.fragment_home, container, false);
gridView=(GridView) view .findViewById(R.id.homeGridView);