Lonergan6275 Lonergan6275 - 5 months ago 33
Android Question

Android NullPointerException in ListView Array Adapter

My app is crashing when i run it with a

NullPointerException
. When i run the app in debug mode i add watchers on all variables in the reported line of code and they all have values in debug mode. i can step over the troubled line in the debugger and it does not crash, but then the app crashes when i return the view (which is also not null according to the debugger) but get no error it just crashes.

Stack Trace

FATAL EXCEPTION: main
Process: com.team.app, PID: 17963
java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.team.app.Adapters.EditableContentListAdapter$ViewHolder.value' on a null object reference
at com.team.app.Adapters.EditableContentListAdapter.getView(EditableContentListAdapter.java:83)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1281)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18880)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18880)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18880)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18880)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:18880)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18880)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18880)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2650)
at android.view.View.measure(View.java:18880)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2104)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1220)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1456)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1111)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6017)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


GetView() Edited

public EditableContentListAdapter(Context context, int layoutResourceId, List<AdvertContent> data) {
super(context, layoutResourceId, data);
mLayoutId = layoutResourceId;
mContext = context;
values = data;
for (int i = 0; i < data.size(); ++i) {
mIdMap.put(data.get(i), i);
}
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {


AdvertContent content = getItem(position);


LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(mLayoutId, parent, false);
viewHolder.value = (TextView) convertView.findViewById(R.id.section_value);
viewHolder.title = (TextView) convertView.findViewById(R.id.section_title);
viewHolder.icon = (ImageView) convertView.findViewById(R.id.section_icon);
viewHolder.value.setText(values.get(position).getValue().toString());
viewHolder.title.setText(values.get(position).getName());
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object

if (content != null) {
viewHolder.title.setText(content.getName()); // carshes on any of these 3 lines in normal run.
viewHolder.value.setText(content.getValue().toString());
viewHolder.title.setTextColor(Color.GRAY);
}
return convertView;//crashes here in debugmode

}


ViewHolder

public static class ViewHolder {
TextView title;
TextView value;
ImageView icon;
}


variables i watched

none reported to be null in debug mode

viewHolder
viewHolder.title
content.getName()
viewHolder.title.getText().toString()
convertView

Answer

In your Inflater you need to give your Layout.

Also you need to set Tag to convertview.

convertView = inflater.inflate(mLayoutId, parent, false);
convertView.setTag(viewHolder);
Comments