Android custom adapter getView() NullPointerException out of nowhere!
I've got a custom Adapter to supply data for ListView. Everything worked fine, but 'suddenly'(I don't have another word for what's now happening) my app started crashing while loading my Activity with listview:
W/dalvikvm( 228): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
E/AndroidRuntime( 228): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 228): java.lang.NullPointerException
E/AndroidRuntime( 228): at AdvertisementListAdapter.getView(AdvertisementListAdapter.java:72)
E/AndroidRuntime( 228): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
E/AndroidRuntime( 228): at android.widget.ListView.measureHeightOfChildren(ListView.java:1147)
E/AndroidRuntime( 228): at android.widget.ListView.onMeasure(ListView.java:1060)
E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964)
E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
E/AndroidRuntime( 228): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
E/AndroidRuntime( 228): at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
E/AndroidRuntime( 228): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
E/AndroidRuntime( 228): 开发者_如何学编程at android.view.View.measure(View.java:7964)
E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
E/AndroidRuntime( 228): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964)
E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
E/AndroidRuntime( 228): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964)
E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
E/AndroidRuntime( 228): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964)
E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
E/AndroidRuntime( 228): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964)
E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
E/AndroidRuntime( 228): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964)
E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
E/AndroidRuntime( 228): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
E/AndroidRuntime( 228): at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
E/AndroidRuntime( 228): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964)
E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
E/AndroidRuntime( 228): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964)
E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
E/AndroidRuntime( 228): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964)
E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
E/AndroidRuntime( 228): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964)
E/AndroidRuntime( 228): at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
E/AndroidRuntime( 228): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
E/AndroidRuntime( 228): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 228): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 228): at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime( 228): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 228): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 228): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 228): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime( 228): at dalvik.system.NativeStart.main(Native Method)
My getView method:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
ViewHolder holder;
if(convertView==null){
view=inflater.inflate(R.layout.list_item, null);
holder=new ViewHolder();
holder.imgItemImage=(ImageView)view.findViewById(R.id.imgItemImage);
holder.tvItemTitle=(TextView)view.findViewById(R.id.tvItemTitle);
holder.tvItemDetails=(TextView)view.findViewById(R.id.tvItemDetails);
view.setTag(holder);
}else
holder=(ViewHolder)view.getTag();
holder.tvItemTitle.setText(data.get(position).getYear()+" "+data.get(position).getTitle());
StringBuilder sb=new StringBuilder();
sb.append(data.get(position).getPrice());
sb.append("\n");
sb.append(data.get(position).getCity());
holder.tvItemDetails.setText(sb.toString());
String imageUrl="http://myuri/"+data.get(position).getUrlList().get(0)+"/p-t.jpeg";
holder.imgItemImage.setTag(imageUrl);
imageLoader.displayImage(imageUrl, activity, holder.imgItemImage);
return view;
}
When getView is first called, convertView=null
, so ViewHolder
object is created. But after calling findViewById(...)
all members of ViewHolder class are still null. So, here are the origins NullPointerException
. View inflating is successul as I can see during debug, view
object contains RelativeLayout
with it's children.
The worst thing is that I haven't changed this part of app before it started crashing!
This is driving me nuts!
Have you tried to rebuild the application from scratch? Project -> Clean
Try rebuilding the R class. In eclipse it's as simple as changing something in the layout XML and saving again to get the R.id.* up to date. Sometimes they get screwed up.
So, that's solved. I tried rebulding, cleaning project. Then I deleted it from workspace and reopened - it worked. That is Eclipse bug, that's horrible, I wasted couple of hours! Thanks to all for responses! Sorry for disturbing...
精彩评论