在Android中使用gridview时,应用程序崩溃了

时间:2014-03-26 10:22:04

标签: android

在我的应用程序中,我想在导航抽屉的片段中使用gridView,但我的应用程序正在崩溃。 这是我的代码:

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); 

    }
}

这是崩溃日志

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 个答案:

答案 0 :(得分:0)

将此行gridView=(GridView) getActivity().findViewById(R.id.homeGridView);放在onCreate中并按此gridView=(GridView) view.findViewById(R.id.homeGridView);执行,因为您的gridview是您的视图的一部分。或者将View视图传递给init();

像这样:

    @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); 

    }

答案 1 :(得分:0)

gridView可能属于片段布局

所以你需要改变这个

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

@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);

答案 2 :(得分:0)

改变这个:

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

到此:

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