我有一个自定义适配器为ListView提供数据。一切正常,但是“突然”(我现在不知道发生什么了)我的应用程序在加载带有Listview的Activity时开始崩溃:
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)
我的getView方法:
@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;
}
首次调用getView
convertView=null
时,将创建ViewHolder
对象。但是在调用findViewById(...)
之后,ViewHolder类的所有成员仍然为空。因此,这是起源NullPointerException
。正如我在调试期间看到的那样,视图膨胀是成功的,view
对象包含RelativeLayout
及其子对象。最糟糕的是,在应用崩溃之前,我还没有对其进行更改!
这真让我发疯!
最佳答案
您是否尝试过从头开始重建应用程序?
项目->清洁
关于android - Android自定义适配器getView()NullPointerException无处不在!,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5955580/