今天用xUtils框架做开发时,遇到一个奇怪的错误。如下所示:
03-29 21:46:13.014: E/SubmitMessage(20680): Submit string: submit:trigger=0,bugtype=2,modulename=com.wangchao.news,level=1,testtype=NORMAL,path=/data/log/unzip/H60-L02_V100R001CHNC00B316_DU2TAN148M015230_20160329214613_crash
03-29 21:46:13.089: E/PackageLogInfoManager(20680): checkPackageLogState, cr: android.app.ContextImpl$ApplicationContentResolver@424ca078, packageNames: null
这个错误是在我用ListView展示数据时发生的。我仔细查看代码后发现,原来是Adapter初始化convertView那个地方出错了。
错误代码如下:
import java.util.List;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.wangchao.news.R;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MenuAdapter extends BaseAdapter {
private Context context;
private List<String> menuList;
private int resource;
public MenuAdapter(Context context,List<String> menuList,int resource){
this.context = context;
this.menuList = menuList;
this.resource = resource;
}
@Override
public int getCount() {
return menuList.size();
}
@Override
public Object getItem(int position) {
return menuList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressWarnings("null")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
holder = new ViewHolder();
View view= View.inflate(context, resource, null);
ViewUtils.inject(holder, view);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.menuListTV.setText(menuList.get(position));
return convertView;
}
public class ViewHolder{
@ViewInject(R.id.menu_listIV)
ImageView menuListIV;
@ViewInject(R.id.menu_listTV)
TextView menuListTV;
}
}
以上代码带下划线且红色字体部分为原因所在。[View.inflate(context, resource, null);]应该直接赋值给convertView。且ViewUtils.inject(holder, view);应改为ViewUtils.inject(holder, convertView);
修改后的getView方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
holder = new ViewHolder();
convertView = View.inflate(context, resource, null);
ViewUtils.inject(holder, convertView);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.menuListTV.setText(menuList.get(position));
return convertView;
}
这样就能正常显示数据了。
另外,使用Gson框架解析Json数据时,如果bean文件中的变量名的大小写与Json中字段的大小写不一致时,也会出现这个错误。
例如:Json数据为
{
"Data": [
{
"BV": 0,
"Count": 0,
"Deleted": 0,
"ID": 143,
"MemberPrice": 500,
"Money": 0,
"Point": 0,
"ProductCharFlag": "4",
"ProductName": "沙朗大師鍋具清潔粉2瓶",
"ProductNumber": "TLF207",
"ProductState": 0,
"ProductType": 4,
"ProductTypeName": "沙朗大師 锅具系列",
"RetailPrice": 600,
"StatusName": "正常",
"ThisTypeCount": 10
},
{
"BV": 0,
"Count": 0,
"Deleted": 0,
"ID": 144,
"MemberPrice": 11250,
"Money": 0,
"Point": 1290,
"ProductCharFlag": "4",
"ProductName": "沙朗大師食物處理機",
"ProductNumber": "TLF208",
"ProductState": 0,
"ProductType": 4,
"ProductTypeName": "沙朗大師 锅具系列",
"RetailPrice": 12500,
"StatusName": "正常",
"ThisTypeCount": 10
},
{
"BV": 0,
"Count": 0,
"Deleted": 0,
"ID": 145,
"MemberPrice": 17820,
"Money": 0,
"Point": 2050,
"ProductCharFlag": "4",
"ProductName": "沙朗大師5夸脫小炒鍋",
"ProductNumber": "TLF209",
"ProductState": 0,
"ProductType": 4,
"ProductTypeName": "沙朗大師 锅具系列",
"RetailPrice": 19800,
"StatusName": "正常",
"ThisTypeCount": 10
}
],
"ModifyDate": "2016-3-14"
}
则其相对应的bean文件就应该定义为:
public class NewsCategory {
public List<Category> Data;
public String ModifyDate;
public static class Category{
public String BV;
public String Count;
public String Deleted;
public String ID;
public String MemberPrice;
public String Money;
public String Point;
public String ProductCharFlag;
public String ProductName;
public String ProductNumber;
public String ProductState;
public String ProductType;
public String ProductTypeName;
public String RetailPrice;
public String StatusName;
public String ThisTypeCount;
}
}