我是新來的Android,目前想創建一個片段顯示的GridView layout.However我無法顯示圖像,並返回下面的錯誤在GridView控件顯示圖像:無法使用片段
java.lang.NullPointerException
at miui.util.AttributeResolver.i(SourceFile:35)
at miui.util.AttributeResolver.resolveInt(SourceFile:145)
at miui.util.AttributeResolver.isUsingMiuiTheme(SourceFile:179)
at miui.os.Environment.isUsingMiui(SourceFile:99)
at android.view.ViewConfigurationInjector.get(ViewConfigurationInjector.java:18)
at android.view.ViewConfigurationHelper.get(ViewConfigurationHelper.java:10)
at android.view.ViewConfiguration.get(ViewConfiguration.java:357)
at android.view.View.<init>(View.java:3452)
at android.widget.ImageView.<init>(ImageView.java:114)
at info.androidhive.slidingmenu.GridViewAdapter.getView(GridViewAdapter.java:52)
at android.widget.AbsListView.obtainView(AbsListView.java:2257)
下面是我的片段代碼
公共類HomeFragment延伸片段{
}
下面是我的gridview的適配器代碼
public class GridViewAdapter extends BaseAdapter {
private Activity _activity;
private LayoutInflater inflater;
private int imageWidth;
private Context context;
public GridViewAdapter(Activity activity) {
this._activity=activity;
}
@Override
public int getCount() {
return this.photoList.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (inflater == null)
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_item_photo, null);
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(photoList[position]);
return imageView;
}
public Integer[] photoList = {
R.drawable.ic_whats_hot, R.drawable.ic_home,
R.drawable.ic_launcher, R.drawable.ic_communities,
R.drawable.ic_people, R.drawable.ic_whats_hot
};
}
錯誤行表明我行從上下文獲取圖像時有問題,可能會建議?
imageView = new ImageView(context);
什麼是第52行? –
你是否動態創建imageview?或者你想在你的佈局中使用imageview – playmaker420
line 52 -imageView = new ImageView(context); – michelletbs