在充气过程中,GridAdapter始终返回空指针异常

时间:2013-12-13 04:23:41

标签: java android android-gridview layout-inflater

您好我有以下代码:

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;
    }

但我仍然得到同样的错误。

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

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

我也100%确定我的ArrayList包含的东西。我试图像它一样Toast it人.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]&lt;&lt;第一次登录
  • 12-13 11:49:23.316:I / THUMBQOO(6671):2&lt;&lt;第二次登录
  • 12-13 11:49:23.316:I / THUMBQOO(6671):andwxyz&lt;&lt;第三个日志

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

5 个答案:

答案 0 :(得分:0)

试试这个,

@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;
 }

答案 1 :(得分:0)

不要在onpostexecute中设置适配器..

gridView.setAdapter(new ContactGridAdapter(getApplicationContext(), result));

尝试将其设置在它之外

答案 2 :(得分:0)

使用doInBackground方法初始化您的arraylist。如果没有获得任何数据,也不要在Adapter类中传递null。它会毫不犹豫地返回空指针。

试试如下:

   try {
        if(resultData != null){
            friendList =new ArrayList<Person>();//declare your arraylist
            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");
    }

<强>编辑:

更改您的getView方法,如下所示:

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

        View view ;
        view =convertView;
        ViewHolder m_holder;
        if (view == null)
        {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         view = inflater.inflate(R.layout.grid_item_text, parent, false);
         m_holder = new ViewHolder();
        m_holder.personName = (TextView) view .findViewById(R.id.textItem);
        m_holder.personThumb = (ImageView) view .findViewById(R.id.iconItem);

       view.setTag( m_holder);    
    }else {
          m_holder = (ViewHolder) view.getTag();
    }

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

   private class ViewHolder {
    private ImageView personThumb ;
    private TextView personName;
}

答案 3 :(得分:0)

Arrayadapter保留自己的Person列表。通过人们在超级ctor的第三个arg然后得到mPeople,getCount和getItem ..或者只是称他们的超级

答案 4 :(得分:0)

试试这会对你有帮助。

我认为问题是在gridView对象中检查它是否为null如果为null,则在为其赋值后重启Aynctask 喜欢

  @Override
protected void onPostExecute(final ArrayList<Person> result) {

     try{
    Log.i("THUMBQOO", result.toString());
    if(result != null){
        gridView.setAdapter(new ContactGridAdapter(getApplicationContext(), result));

     catch(Exception e){
         e.printStackTrace();
       //  your code for  grid view if it is null than restart Asyctask or regenerate 
       //     gridview object
      }
    }

如果您需要任何帮助,请告诉我