错误 BaseExpandableListAdapter textview null

Error BaseExpandableListAdapter textview null

我有一个问题,我正在做一个 android 应用程序,我想列出一个问题,当我们点击一​​个问题时,它会滚动一个答案列表(在一个片段中)。 我做了一个自定义适配器,我想在每个问题的答案列表后添加一个页脚(我想添加一个编辑文本来添加一个答案)。

当我滚动一个问题的答案列表时,它与页脚配合得很好,但是当我尝试第二次滚动同一列表时,他告诉我文本视图为空...

这里是代码:

-> adapter


public class MyExpandableAdapter extends BaseExpandableListAdapter {
private Context context;
private LinkedList<Question> groups;

public MyExpandableAdapter(Context context, LinkedList<Question> groups)
{
    this.context = context;
    this.groups = groups;
}
@Override
public int getGroupCount() {
    return groups.size();
}

//Add 2 to childcount. The first row and the last row are used as header and footer to childview
@Override
public int getChildrenCount(int i) {
    return groups.get(i).getListAnswer().size() + 1 ;
}

@Override
public Question getGroup(int i) {
    return groups.get(i);
}

@Override
public Answer getChild(int i, int i2) {
    return groups.get(i).getListAnswer().get(i2);
}

@Override
public long getGroupId(int i) {
    return i;
}

@Override
public long getChildId(int i, int i2) {
    return 0;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup)
{
    Question currentQuestion = groups.get(i);
    if(view == null)
    {
        LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.listquestion_home,null);
    }
    ((CheckedTextView) view.findViewById(R.id.listrow_group_question)).setText(currentQuestion.getQuestion());
    ((CheckedTextView) view.findViewById(R.id.listrow_group_question)).setChecked(true);
    ((TextView) view.findViewById(R.id.listrow_group_author)).setText("Anonymous" + currentQuestion.getUserId().toString());
    return view;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //Here is the ListView of the ChildView

    if (childPosition < getChildrenCount(groupPosition) - 1)
    {
        Answer answer = getChild(groupPosition, childPosition);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.listanswer_home, null);
        }

        TextView text = (TextView) convertView.findViewById(R.id.textView1);
        text.setText(answer.getAnswer());

        //the last row is used as footer
    }
    else
    {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.test_edit_text, null);
        }
    }

    return convertView;
}

@Override
public boolean isChildSelectable(int i, int i2) {
    return false;
}

}

-> 片段代码:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.home_fragment, container, false);
    ExpandableListView listView = (ExpandableListView) rootView.findViewById(R.id.listView);
    MyExpandableAdapter adapter = new MyExpandableAdapter(getActivity(), QuestionManager.getQuestionManager().getQuestionWithAnswerNotFromMe());
    listView.setAdapter(adapter);
    return rootView;
}

XML 代码:

-> 回答

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="40dp"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawablePadding="5dp"
    android:gravity="center_vertical"
    android:text="hey"
    android:textSize="14sp"
    android:textStyle="bold" >
</TextView>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/black" />

-> 问题

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
>

<CheckedTextView
    android:id="@+id/listrow_group_question"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_marginLeft="8dp"
    android:gravity="left"
    android:paddingLeft="32dp"
    android:paddingTop="8dp"
    android:text="Test"
    android:textSize="14sp"
    android:textStyle="bold"

    />

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listrow_group_author"
    android:layout_below="@id/listrow_group_question"
    android:gravity="right"

    />

-> 页脚

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
>

<TextView
    android:id="@+id/txtFooter"
    android:textColor="@android:color/holo_green_light"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center_vertical"
    android:textStyle="bold"
    android:text="Footer" />

错误在适配器中: text.setText(answer.getAnswer());

我真的不明白。我为它添加了一个子尺寸,但是当我这样做时出现错误...

如果你有任何想法:'( !

谢谢

日志:

01-06 15:03:39.845 23899-23899/com.example.thibault.project_mobile E/AndroidRuntime: java.lang.NullPointerException: 尝试在空对象上调用虚拟方法 'void android.widget.TextView.setText(java.lang.CharSequence)'参考

01-06 15:03:39.845 23899-23899/com.example.thibault.project_mobile E/AndroidRuntime: 在 com.example.thibault.project_mobile.MyExpandableAdapter.getChildView(MyExpandableAdapter.java:92)

01-06 15:03:39.845 23899-23899/com.example.thibault.project_mobile E/AndroidRuntime: 在 android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:451)

The error is in the adapter : text.setText(answer.getAnswer());

因为 TextViewtextView1 id 仅在 listanswer_home.xml 布局中可用而不在 test_edit_text.xml 中并且如果 if (childPosition < getChildrenCount(groupPosition) - 1) 条件是 false 重新调整test_edit_text.xml 布局来自 getChildViewconvertView.findViewById(R.id.textView1); return null.

在调用 TextViewsetText 方法之前添加 null 检查,如:

 if(text !=null){
    text.setText(answer.getAnswer());
  }

或在listanswer_home.xml中添加TextViewtextView1