我是Android新手,目前想要创建一个片段来显示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)
下面是我的片段代码
public class HomeFragment扩展了Fragment {
public HomeFragment(){}
private GridViewAdapter adapter;
private GridView gridView;
private int columnWidth;
private Utils utils;
private PrefManager pref;
private Integer[] photosList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
gridView = (GridView) rootView.findViewById(R.id.grid_view);
gridView.setVisibility(View.VISIBLE);
gridView.setAdapter(new GridViewAdapter(getActivity()));
return rootView;
}
}
以下是我的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);
答案 0 :(得分:1)
您的上下文对象为null。 因为你有活动的背景。所以使用这个
imageView = new ImageView(_activity);
答案 1 :(得分:0)
由于您的上下文返回null,我们需要提供一个非null的参数。一些提供上下文的常见类是Activity,Application,Service和View。在第9行中,初始化一个Activity对象_activity。
由于此对象具有上下文,因此您可以使用它来代替“上下文”。
imageView = new ImageView(_activity);
答案 2 :(得分:0)
您正在使用未初始化的上下文。
请使用从HomeFragment通过GridViewAdapter解析的_activity。
错误是你的&#34; context&#34;从未在GridViewAdapter中初始化。
替换为imageView = new ImageView(_activity);