创建gridview时出现空指针异常

时间:2014-07-31 06:01:10

标签: android gridview

我想做什么:我正在gridview中显示图片

正在发生的事情 ::在

获得null-pointer-exception
        flagNameId = (TextView) base.findViewById(R.id.flagNameId);

我已正确引用了ID,但仍有异常,我该如何解决?


CustomGrid.java

public class CustomGrid extends BaseAdapter{

    private Context mContext;
    LinkedList<LinkedHashMap<String, Integer>> messengerListObject;

    public CustomGrid(Context _mContext,LinkedList<LinkedHashMap<String, Integer>> _messengerListObject) {
        messengerListObject=_messengerListObject;
        mContext=_mContext;
    }

    @Override
    public int getCount() {
        return messengerListObject.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    class ViewHolder {
        public TextView flagNameId;
        public ImageView flagImgId;

        public ViewHolder(View base) {
            flagNameId = (TextView) base.findViewById(R.id.flagNameId);
            flagImgId = (ImageView) base.findViewById(R.id.flagImgId);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;  
        String countryName = "";
        int countryFlag = 0;
        ViewHolder vHolder;  

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vHolder = new ViewHolder(view);

        if (convertView == null) {
            vHolder = new ViewHolder(view);
            view = inflater.inflate(R.layout.grid_single, null);
            view.setTag(vHolder);  
        } else {  
            vHolder = (ViewHolder) view.getTag();  
        }

        HashMap<String, Integer> messengerMapObject= messengerListObject.get(position);


        for (Entry<String, Integer> entry : messengerMapObject.entrySet()) {
            countryFlag = entry.getValue();

            vHolder.flagNameId.setText(entry.getKey());
            vHolder.flagImgId.setImageResource(entry.getValue());
        }

        return view;
    }
}

MainActivity.java

public class MainActivity extends Activity {

    GridView grid;

    LinkedList<LinkedHashMap<String, Integer>> messengerListObject;
    LinkedHashMap<String, Integer> messengerMapObject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        grid=(GridView) findViewById(R.id.grid);

        messengerListObject=new LinkedList<LinkedHashMap<String, Integer>>();
        messengerMapObject=new LinkedHashMap<String, Integer>();

        //Add the data
        messengerMapObject.put("Canada", R.drawable.canada);
        messengerMapObject.put("China", R.drawable.china);
        messengerMapObject.put("England", R.drawable.england);
        messengerMapObject.put("Holland", R.drawable.holland);
        messengerMapObject.put("India", R.drawable.india);
        messengerMapObject.put("Russia", R.drawable.russia);
        messengerMapObject.put("Spain", R.drawable.spain);
        messengerMapObject.put("Usa", R.drawable.usa);

        messengerListObject.add(messengerMapObject);


        CustomGrid adapter = new CustomGrid(MainActivity.this,messengerListObject);
        grid.setAdapter(adapter);
        /*grid=(GridView)findViewById(R.id.grid);
        grid.setAdapter(adapter);
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                //Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
            }
        });*/
    }
}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:listSelector="#00000000"
        android:numColumns="auto_fit" />

</LinearLayout>

grid_single.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#222222"
    android:orientation="vertical"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#E6E6E6"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/grid_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/eleven" >
                </ImageView>
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#FFFFFF"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/grid_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Text"
                    android:textSize="12sp" >
                </TextView>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</TableLayout>

日志::

07-31 11:25:33.365: E/AndroidRuntime(7963): FATAL EXCEPTION: main
07-31 11:25:33.365: E/AndroidRuntime(7963): java.lang.NullPointerException
07-31 11:25:33.365: E/AndroidRuntime(7963):     at com.example.grid.CustomGrid$ViewHolder.<init>(CustomGrid.java:46)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at com.example.grid.CustomGrid.getView(CustomGrid.java:60)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.widget.AbsListView.obtainView(AbsListView.java:2033)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.widget.GridView.onMeasure(GridView.java:1026)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.view.View.measure(View.java:12603)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1017)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:555)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.view.View.measure(View.java:12603)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.view.View.measure(View.java:12603)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.view.View.measure(View.java:12603)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2072)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.view.View.measure(View.java:12603)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1044)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.os.Looper.loop(Looper.java:137)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at android.app.ActivityThread.main(ActivityThread.java:4340)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at java.lang.reflect.Method.invoke(Method.java:511)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-31 11:25:33.365: E/AndroidRuntime(7963):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

您的ViewHolder构造函数在传入视图中调用方法,但您尚未确保该视图为非null。

更改

vHolder = new ViewHolder(view);

if (convertView == null) {
    vHolder = new ViewHolder(view);
    view = inflater.inflate(R.layout.grid_single, null);

类似

if (convertView == null) {
    view = inflater.inflate(R.layout.grid_single, null);
    vHolder = new ViewHolder(view);

即。删除第一个冗余的ViewHolder实例,并在您夸大视图后重新排序另一个实例。

答案 1 :(得分:0)

在使用

之前,您没有初始化“视图”

写下这样的东西:

view = inflater.inflate(R.layout.grid_single, null);
vHolder = new ViewHolder(view);