嗨,我有以下代码:

public class ContactGridAdapter extends ArrayAdapter<Person> {
    private Context mContext;
    private ArrayList<Person> mPeople;
    private int mThumbs = R.drawable.default_photo_ic;

    public ContactGridAdapter(Context context, ArrayList<Person> people){
        super(context, R.layout.grid_item_text);
        mContext = context;
        mPeople = people;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (mPeople == null){
            return 0;
        }else{
            return mPeople.size();
        }
    }

    @Override
    public Person getItem(int position) {
        // TODO Auto-generated method stub
        return mPeople.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.grid_item_text, parent, false);

        TextView personName = (TextView) view.findViewById(R.id.textItem);
        ImageView personThumb = (ImageView) view.findViewById(R.id.iconItem);

        personName.setText(mPeople.get(position).fullName);
        personThumb.setImageResource(mThumbs);
        return view;
    }
}


像这样在onPostExecute中从MainActivity调用它。

*** REST OF CODE OMITTED ***
private class RetrieveFriendList extends AsyncTask<String, Void, ArrayList<Person>> {

    @Override
    protected ArrayList<Person> doInBackground(String... params) {
        // TODO Auto-generated method stub
        ArrayList<Person> friendList = null;
        String resultData = ((new HttpRetriever())).getFriends(myAccount.email, myAccount.passwd);
        Log.i("THUMBQOO", resultData);

        try {
            if(resultData != null){
                friendList = (new ContactHandlers()).friendList(resultData);
            }else{
                friendList = null;
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.e("THUMBQOO - RET FRIEND", "JSON Exception");
        }

        return friendList;
    }

    @Override
    protected void onPostExecute(final ArrayList<Person> result) {
        Log.i("THUMBQOO", result.toString());
        if(result != null){
            gridView.setAdapter(new ContactGridAdapter(getApplicationContext(), result));
        }
    }
}


我怎么总是在上面的gridView.setAdapter行上得到java.lang.NullPointerException。

我试图删除适配器中的上下文引用,所以我的代码如下所示:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.grid_item_text, null);

        TextView personName = (TextView) view.findViewById(R.id.textItem);
        ImageView personThumb = (ImageView) view.findViewById(R.id.iconItem);

        personName.setText(mPeople.get(position).fullName);
        personThumb.setImageResource(mThumbs);
        return view;
    }


但是我仍然遇到同样的错误。

我确信通过从网格构造函数中打印一些Log信息来创建ContactGridAdapter对象。

该错误源于geView()中的某个地方,但是我不确定具体在哪里。

我还100%确保我的ArrayList包含某些内容。我曾尝试像Toast那样敬酒people.get(0).ObjectVariable

谁能帮助我指出正确的方向?

编辑1:

这是我的LogCat的摘录

12-13 11:17:05.971: E/AndroidRuntime(4722): FATAL EXCEPTION: main
12-13 11:17:05.971: E/AndroidRuntime(4722): java.lang.NullPointerException
12-13 11:17:05.971: E/AndroidRuntime(4722):     at com.gelliesmedia.thumbqoo.ContactActivity$RetrieveFriendList.onPostExecute(ContactActivity.java:239)
12-13 11:17:05.971: E/AndroidRuntime(4722):     at com.gelliesmedia.thumbqoo.ContactActivity$RetrieveFriendList.onPostExecute(ContactActivity.java:1)
12-13 11:17:05.971: E/AndroidRuntime(4722):     at android.os.AsyncTask.finish(AsyncTask.java:631)
12-13 11:17:05.971: E/AndroidRuntime(4722):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-13 11:17:05.971: E/AndroidRuntime(4722):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-13 11:17:05.971: E/AndroidRuntime(4722):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 11:17:05.971: E/AndroidRuntime(4722):     at android.os.Looper.loop(Looper.java:137)
12-13 11:17:05.971: E/AndroidRuntime(4722):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-13 11:17:05.971: E/AndroidRuntime(4722):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 11:17:05.971: E/AndroidRuntime(4722):     at java.lang.reflect.Method.invoke(Method.java:511)
12-13 11:17:05.971: E/AndroidRuntime(4722):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-13 11:17:05.971: E/AndroidRuntime(4722):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-13 11:17:05.971: E/AndroidRuntime(4722):     at dalvik.system.NativeStart.main(Native Method)


编辑2:

谢谢大家的建议,但是问题仍然存在。

出于调试目的,这是我所做的:

    @Override
    protected void onPostExecute(final ArrayList<Person> result) {
        StringBuilder sb = new StringBuilder();
        sb.append("");
        sb.append(result.size());
        String rSize = sb.toString();

        Log.i("THUMBQOO", result.toString());
        Log.i("THUMBQOO", rSize);
        Log.i("THUMBQOO", result.get(0).fullName.toString());


        /*
         * if(result != null){
            gridView.setAdapter(new ContactGridAdapter(getApplicationContext(), result));
        }*/
    }


这就是我从LogCat得到的


12-13 11:49:23.316:I / THUMBQOO(6671):
[com.gelliesmedia.thumbqoo.models.Person@a7446248,
com.gelliesmedia.thumbqoo.models.Person@a7485d38] <12-13 11:49:23.316:I / THUMBQOO(6671):2 <12-13 11:49:23.316:I / THUMBQOO(6671):andwxyz <

基本上表明结果确实不是null。

最佳答案

尝试这个,,

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View row ;
        if (convertView == null)
        {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.grid_item_text, null);

        TextView personName = (TextView) row.findViewById(R.id.textItem);
        ImageView personThumb = (ImageView) row.findViewById(R.id.iconItem);

        personName.setText(mPeople.get(position).fullName);
        personThumb.setImageResource(mThumbs);

    }else {
    row = (View) convertView;
    }
   return row;
 }

10-08 03:14