I am encoding a custom adapter for a ListView that has a custom list.
obviously i have to write getView
function. so here is my code:
public View getView(int position, View convertView, ViewGroup parent) { View vi = convertView; if (convertView == null) vi = inflater.inflate(R.layout.result_list_item, null); TextView title = (TextView) vi.findViewById(R.id.tvTitle); TextView subtitle = (TextView) vi.findViewById(R.id.tvSubTitle); ImageView image = (ImageView) vi.findViewById(R.id.imageView1); title.setText(data.get(position).getDescription().toString()); subtitle.setText(data.get(position).getDate().toString()); String img = "http://someLink/" + data.get(position).getId() + "a_t.jpg"; imageLoader.DisplayImage(img, image); return vi; }
But I noticed that on line number 6 the program crashes due to a NullPointer exception.
here is my crash report:
04-18 09:33:32.745: E/AndroidRuntime(2209): FATAL EXCEPTION: main 04-18 09:33:32.745: E/AndroidRuntime(2209): java.lang.NullPointerException 04-18 09:33:32.745: E/AndroidRuntime(2209): at com.package.mojo.NoPicAdapter.getView(NoPicAdapter.java:46) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.widget.AbsListView.obtainView(AbsListView.java:1554) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.widget.ListView.measureHeightOfChildren(ListView.java:1264) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.widget.ListView.onMeasure(ListView.java:1175) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.view.View.measure(View.java:8366) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.view.View.measure(View.java:8366) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.view.View.measure(View.java:8366) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.view.View.measure(View.java:8366) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.view.View.measure(View.java:8366) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.view.ViewRoot.performTraversals(ViewRoot.java:847) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.view.ViewRoot.handleMessage(ViewRoot.java:1868) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.os.Handler.dispatchMessage(Handler.java:99) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.os.Looper.loop(Looper.java:123) 04-18 09:33:32.745: E/AndroidRuntime(2209): at android.app.ActivityThread.main(ActivityThread.java:3691) 04-18 09:33:32.745: E/AndroidRuntime(2209): at java.lang.reflect.Method.invokeNative(Native Method) 04-18 09:33:32.745: E/AndroidRuntime(2209): at java.lang.reflect.Method.invoke(Method.java:507) 04-18 09:33:32.745: E/AndroidRuntime(2209): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 04-18 09:33:32.745: E/AndroidRuntime(2209): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 04-18 09:33:32.745: E/AndroidRuntime(2209): at dalvik.system.NativeStart.main(Native Method)
Of course, the layout and elements themselves exist ...
any thoughts?
update: when I look at variables during debugging, I see that vi
not overestimated ( vi == null
), why?
source share