gridview NullPointerException simpleAdapter

Posted

技术标签:

【中文标题】gridview NullPointerException simpleAdapter【英文标题】: 【发布时间】:2017-01-08 18:44:32 【问题描述】:
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 ComponentInfocom.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 

【问题讨论】:

不是因为gridview!!!您的 Fragment 有问题,请您将 android.support.v4.app.Fragment 替换为 android.app.Fragment 并试一试 我只使用 android.support.v4.app.Fragment 因为 android.support.v4.app.FragmentManager; android.support.v4.app.FragmentTransaction; What is a NullPointerException, and how do I fix it?的可能重复 【参考方案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); 

【讨论】:

以上是关于gridview NullPointerException simpleAdapter的主要内容,如果未能解决你的问题,请参考以下文章

如何获取gridview单元格的值

GridView 72般绝技

GridView 问题

怎样获取gridview中数据总行数

在 Gridview 中显示 Gridview

在Gridview中显示Gridview