gpt4 book ai didi

java - GridAdapter在膨胀过程中总是返回空指针异常

转载 作者:太空宇宙 更新时间:2023-11-04 15:19:27 24 4
gpt4 key购买 nike

嗨,我有以下代码:

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 包含某些内容。我尝试像 people.get(0).ObjectVariable 那样 Toast 它

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

编辑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:我/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 << 第三条日志

这基本上表明结果确实不为空。

最佳答案

试试这个,,

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

关于java - GridAdapter在膨胀过程中总是返回空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20559050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com