在Android中使用GridView时出现异常

时间:2013-11-25 00:33:04

标签: android gridview

我正在尝试使用GridView创建一个联系人页面,每个人的图片,当我尝试运行它时,我的程序崩溃了。

这是contacts_activity代码:

    package com.example.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import java.util.ArrayList;

public class contacts_activity extends Activity {

    GridView mContactsGrid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);
        mContactsGrid= (GridView) findViewById(R.id.gridView);
        mContactsGrid.setAdapter(new contactsAdapter(this));
    }
    //This class bounds each image of a contact with each name
    class contacts{
        int mContactImageID;
        String mContactName;

        //constructor
        contacts(int mContactImageID, String mContactName){
            this.mContactImageID=mContactImageID;
            this.mContactName=mContactName;

        }
    }

    //The contactsAdapter will only take one array
    class contactsAdapter extends BaseAdapter{

        ArrayList<contacts> list;
        Context context;
        contactsAdapter(Context context){
            this.context = context;
            list = new ArrayList<contacts>();
            Resources res = context.getResources();
            String[] mTempContactsNames = res.getStringArray(R.array.contacts_names);
            int[] mContactsImages = {R.drawable.female_a,
                                     R.drawable.female_b,
                                     R.drawable.female_c,
                                     R.drawable.female_d,
                                     R.drawable.female_e,
                                     R.drawable.female_f,
                                     R.drawable.female_g,
                                     R.drawable.female_h,
                                     R.drawable.female_i,
                                     R.drawable.female_j,
                                     R.drawable.female_k,
                                     R.drawable.female_l,
                                     R.drawable.female_m,
                                     R.drawable.female_n,
                                     R.drawable.men_a,
                                     R.drawable.men_b,
                                     R.drawable.men_c,
                                     R.drawable.men_d,
                                     R.drawable.men_e,
                                     R.drawable.men_f,
                                     R.drawable.men_g,
                                     R.drawable.men_h,
                                     R.drawable.men_i,
                                     R.drawable.men_j,
                                     R.drawable.men_k,};

            for(int i =0; i<mContactsImages.length; i++){
                contacts mTempContacts = new contacts(mContactsImages[i], mTempContactsNames[i]);
                list.add(mTempContacts);
            }

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

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

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

        class mViewHolder{

            ImageView myContact;
            mViewHolder(View v){
                myContact = (ImageView) findViewById(R.id.imageView);
            }
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            View row = view;
            mViewHolder holder = null;
            if(row==null){
                LayoutInflater inflater = (LayoutInflater)    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.single_item,viewGroup,false);
                holder = new mViewHolder(row);
                row.setTag(holder);
            }
            else{
                holder = (mViewHolder) row.getTag();

            }
            contacts temp = list.get(i);
            holder.myContact.setImageResource(temp.mContactImageID);
            return row;
        }
    }

}

这里是activity_layout xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="#0f3f6d">

    <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/gridView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="74dp"
        android:numColumns="auto_fit"
        android:horizontalSpacing="1dp"
        android:verticalSpacing="1dp"
        android:columnWidth="120dp"
        android:stretchMode="columnWidth"/>

</RelativeLayout>

logcat的:

11-24 20:13:48.845  13386-13386/com.example.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.myapplication.contacts_activity$contactsAdapter.getView(contacts_activity.java:127)
            at android.widget.AbsListView.obtainView(AbsListView.java:2613)
            at android.widget.GridView.onMeasure(GridView.java:1043)
            at android.view.View.measure(View.java:16059)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
            at android.view.View.measure(View.java:16059)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4923)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16059)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
            at android.view.View.measure(View.java:16059)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4923)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2414)
            at android.view.View.measure(View.java:16059)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2129)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1282)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1493)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1179)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4859)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
            at android.view.Choreographer.doCallbacks(Choreographer.java:562)
            at android.view.Choreographer.doFrame(Choreographer.java:532)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5328)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)

程序编译,但在我尝试转到联系人活动时停止。 请帮忙 感谢

1 个答案:

答案 0 :(得分:1)

您在第85行收到ArrayIndexOutOfBounds个例外。  我猜mTempContacts[]只有16项,比你的mContactImages[]数组少

可以在for循环初始化中进行一次修复,而不是使用

for (int i = 0; i < mContactsImages.length; i++){

你可以使用

for (int i = 0; i < mTempContacts.length; i++){

但是,如果您的联系人数多于联系人图像,那么这也会失败。