I work on the ExpandableList adapter, but my app getting crush by NullPointerException. I am wondering what could cause the viewHolder not to set up correctly.
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolder vh;
//Set up layout for ExpandableList
if(groupPosition==0){
convertView =LayoutInflater.from(context).inflate(R.layout.friends_list_title,null);
}else{
if(convertView==null){
convertView= LayoutInflater.from(context).inflate(R.layout.friends_list_item,null);
vh = new ViewHolder();
vh.tv = (TextView) convertView.findViewById(R.id.group_tv);
convertView.setTag(vh);
}
vh = (ViewHolder) convertView.getTag();
vh.tv.setText(group[groupPosition]);
}
return convertView;
}
2021-03-12 14:17:38.006 8189-8189/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myapplication, PID: 8189 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.MyExpandableListAdapter.getGroupView(ExpamdListAdapter.java:81) at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446) at android.widget.AbsListView.obtainView(AbsListView.java:2387) at android.widget.ListView.makeAndAddView(ListView.java:2067) at android.widget.ListView.fillDown(ListView.java:793) at android.widget.ListView.fillFromTop(ListView.java:855) at android.widget.ListView.layoutChildren(ListView.java:1810) at android.widget.AbsListView.onLayout(AbsListView.java:2184) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673) at android.widget.LinearLayout.onLayout(LinearLayout.java:1582) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) at android.widget.FrameLayout.onLayout(FrameLayout.java:270) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673) at android.widget.LinearLayout.onLayout(LinearLayout.java:1582) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) at android.widget.FrameLayout.onLayout(FrameLayout.java:270) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:530) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) at android.widget.FrameLayout.onLayout(FrameLayout.java:270) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673) at android.widget.LinearLayout.onLayout(LinearLayout.java:1582) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) at android.widget.FrameLayout.onLayout(FrameLayout.java:270) at com.android.internal.policy.DecorView.onLayout(DecorView.java:784) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3470) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2938) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972) at android.view.Choreographer.doCallbacks(Choreographer.java:796) at android.view.Choreographer.doFrame(Choreographer.java:731) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 2021-03-12 14:17:40.138 8189-8189/? I/Process: Sending signal. PID: 8189 SIG: 9
(ViewHolder) convertView.getTag();
. – leo