当我尝试使用expandable getgroupview方法在textview中设置值时,我遇到了一些错误。请建议做什么,因为我无法设置值
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
CommentModel commentModel = (CommentModel) getGroup(groupPosition);
ViewHolder viewholder=null;
if (convertView==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.commentparent,null);
viewholder = new ViewHolder();
Log.i("MYNEWSAMSING",commentModel.getContent());
viewholder.parentname = (TextView) convertView.findViewById(R.id.parentname);
convertView.setTag(viewholder);
}else{
viewholder=(ViewHolder)convertView.getTag();
}
viewholder.parentname.setText(commentModel.getContent());
return convertView;
}
这是我在运行程序时得到的错误。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mytrendin.simple, PID: 8149
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.mytrendin.simple.Expandablelistadapter.getGroupView(Expandablelistadapter.java:87)
at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
at android.widget.AbsListView.obtainView(AbsListView.java:2362)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1326)
at android.widget.ListView.onMeasure(ListView.java:1233)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:687)
at android.view.View.measure(View.java:19734)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2271)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1358)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1607)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
0
答案 0 :(得分:1)
问题出在if语句中。
如果看到代码,则在下面的语句中,convertView为null
viewholder.parentname = (TextView) convertView.findViewById(R.id.parentname);
以下是if条件的正确实现:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.commentparent,null);
viewholder = new ViewHolder();
Log.i("MYNEWSAMSING",commentModel.getContent());
viewholder.parentname = (TextView) convertView.findViewById(R.id.parentname);
convertView.setTag(viewholder);
答案 1 :(得分:0)
您尚未引用 View
方法返回的 inflate()
。
在 View
convertView
对象
更改为以下内容:
if (convertView==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.commentparent,null);
viewholder = new ViewHolder();
Log.i("MYNEWSAMSING",commentModel.getContent());
viewholder.parentname = (TextView) convertView.findViewById(R.id.parentname);
convertView.setTag(viewholder);
}