gridview NullPointerException simpleAdapter

时间:2016-09-01 03:08:12

标签: android gridview nullpointerexception simpleadapter

package com.rong.shop.myshop.home;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import com.rong.shop.R;    
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/13.
 */
public class MyFragment_home extends Fragment {

    //ViewPager
    private ViewPager vpager_one;
    private ArrayList<View> aList;
    private Adapter_homeViewPager mAdapter;

    //GridView
    private GridView gridview;
    private Context mcontext = this.getActivity();
    private List<Map<String,Object>> listitem;
    private SimpleAdapter gAdapter;
    private int[] imgIds;

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

        //ViewPager
        vpager_one = (ViewPager) view.findViewById(R.id.vpager);

        aList = new ArrayList<View>();
        LayoutInflater li = getLayoutInflater(null);
        aList.add(li.inflate(R.layout.fg_home_viewpager_view_1, null, false));
        aList.add(li.inflate(R.layout.fg_home_viewpager_view_2, null, false));
        aList.add(li.inflate(R.layout.fg_home_viewpager_view_3, null, false));
        aList.add(li.inflate(R.layout.fg_home_viewpager_view_4, null, false));
        aList.add(li.inflate(R.layout.fg_home_viewpager_view_5, null, false));

        mAdapter = new Adapter_homeViewPager(aList);
        vpager_one.setAdapter(mAdapter);    

        //GridView
        gridview = (GridView)view.findViewById(R.id.gridview);

        listitem= new ArrayList<>();
        imgIds = new int[]{
                R.drawable.fg_home_gridview_1, R.drawable.fg_home_gridview_2,
                R.drawable.fg_home_gridview_3, R.drawable.fg_home_gridview_4,
                R.drawable.fg_home_gridview_5, R.drawable.fg_home_gridview_6,
                R.drawable.fg_home_gridview_7, R.drawable.fg_home_gridview_8,
        };
        for (int i = 0; i < imgIds.length; i++) {
            Map<String, Object> showitem = new HashMap<String, Object>();
            showitem.put("image", imgIds[i]);
            listitem.add(showitem);
        }

        gAdapter = new SimpleAdapter(mcontext, listitem, R.layout.fg_home_gridview_item,
                new String[] { "image" }, new int[]{R.id.gridview_image});
        gridview.setAdapter(gAdapter);   

        return view;
    }
}

例外:

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rong.shop/com.rong.shop.myshop.home.MainActivity}: java.lang.NullPointerException
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
    at android.app.ActivityThread.access$800(ActivityThread.java:144)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5146)
    at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:515)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
    at dalvik.system.NativeStart.main(Native Method)
                                                      Caused by: java.lang.NullPointerException
     at android.widget.SimpleAdapter.<init>(SimpleAdapter.java:85)
    at com.rong.shop.myshop.home.MyFragment_home.onCreateView(MyFragment_home.java:75)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
     at 
    android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
        at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:619)
      at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
      at android.app.Activity.performStart(Activity.java:5241)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271) 
   at 

android.app.ActivityThread.access$800(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:136) 
     at android.app.ActivityThread.main(ActivityThread.java:5146) 
       at 

1 个答案:

答案 0 :(得分:1)

您正在片段实例化中初始化mcontext,并调用getActivity。那时候还没有活动,因此mcontext为空,因此SimpleAdapter失败 您应该完全摆脱mcontext,因为它不需要,并且只会导致问题。只需使用getActivity代替。所以只需将其更改为;

gAdapter = new SimpleAdapter(getActivity(), listitem, R.layout.fg_home_gridview_item, new String[] { "image" }, new int[]{R.id.gridview_image});