我已经使用这个网站多年了,但这是我第一次无法找到问题的答案。我正在关注一个在线教程,以便从RSS源向我的应用添加图像缩略图。我一直在从我的convertView实例化中遇到一个NULL指针错误,但它实际上是如何实例化教程的,所以我不明白为什么会这样。
public class PostItemAdapter extends ArrayAdapter<PostData> {
private Activity myContext;
private ArrayList<PostData> datas;
private String[] imageURLArray;
LayoutInflater inflater;
static class ViewHolder {
TextView postTitleView;
TextView postDateView;
ImageView postThumbView;
String postThumbViewURL;
Bitmap bitmap;
}
public PostItemAdapter(Context context, int textViewResourceId,
ArrayList<PostData> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
myContext = (Activity) context;
datas = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.postitem, null);
viewHolder = new ViewHolder();
viewHolder.postThumbView = (ImageView) convertView.findViewById(R.id.postThumb);
viewHolder.postTitleView = (TextView) convertView.findViewById(R.id.postTitleLabel);
viewHolder.postDateView = (TextView) convertView.findViewById(R.id.postDateLabel);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
PostData post = datas.get(position);
if (post.postThumbUrl != null) {
viewHolder.postThumbViewURL = post.postThumbUrl;
new DownloadImageTask().execute(viewHolder);
} else {
viewHolder.postThumbView.setImageResource(R.drawable.ic_action_event);
}
viewHolder.postTitleView.setText(post.postTitle);
viewHolder.postDateView.setText(post.postDate);
new DownloadImageTask().execute(viewHolder);
return convertView;
}
}
堆栈追踪:
04-24 13:32:12.748 7335-7335/com.chuckfranklin.chuckfranklinlaw E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.chuckfranklin.chuckfranklinlaw, PID: 7335
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup)' on a null object reference
at com.chuckfranklin.chuckfranklinlaw.PostItemAdapter.getView(PostItemAdapter.java:56)
at android.widget.AbsListView.obtainView(AbsListView.java:2825)
at android.widget.ListView.makeAndAddView(ListView.java:1884)
at android.widget.ListView.fillDown(ListView.java:713)
at android.widget.ListView.fillFromTop(ListView.java:779)
at android.widget.ListView.layoutChildren(ListView.java:1679)
at android.widget.AbsListView.onLayout(AbsListView.java:2629)
at android.view.View.layout(View.java:16694)
at android.view.ViewGroup.layout(ViewGroup.java:5328)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
at android.view.View.layout(View.java:16694)
at android.view.ViewGroup.layout(ViewGroup.java:5328)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:16694)
at android.view.ViewGroup.layout(ViewGroup.java:5328)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
at android.view.View.layout(View.java:16694)
at android.view.ViewGroup.layout(ViewGroup.java:5328)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:16694)
at android.view.ViewGroup.layout(ViewGroup.java:5328)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
at android.view.View.layout(View.java:16694)
at android.view.ViewGroup.layout(ViewGroup.java:5328)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:16694)
at android.view.ViewGroup.layout(ViewGroup.java:5328)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2321)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2034)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6641)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
答案 0 :(得分:0)
与日志一样:
NullPointerException:尝试调用虚方法 &#39; android.view.View android.view.LayoutInflater.inflate
显然,使用inflate
的非初始化对象调用LayoutInflater
方法。将其初始化为:
inflater = (LayoutInflater)
myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
答案 1 :(得分:0)
您忘记初始化inflater
。由于您在构造函数中已获得Context
,因此您可以通过以下方式执行此操作:
inflater = LayoutInflater.from(context);
当然,它应该添加到构造函数中,而不是getView
。
答案 2 :(得分:0)
您无法在不初始化的情况下访问会员。
public PostItemAdapter(Context context, int textViewResourceId, ArrayList<PostData> objects) {
super(context, textViewResourceId, objects);
myContext = (Activity) context;
inflater = Layourinflater.from(context);
datas = objects;
}
答案 3 :(得分:0)
您的inflater永远不会被初始化,因此您会收到此错误。
使用此
inflater = LayoutInflater.from(context);
答案 4 :(得分:0)
在初始化convertview之前,在getView()中添加一行代码,如下所示:
if (convertView == null) {
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.postitem, null);
}