错误BaseExpandableListAdapter textview null

时间:2016-01-06 14:32:04

标签: android adapter

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

当我滚动一个问题的答案列表时,它与页脚的效果很好但是当我尝试第二次滚动同一个列表时,他说我的textview为空...

这里是代码:

-> 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;
}

}

- &GT;片段代码:

    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代码:

- &GT;答案

<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" />

- &GT;问题

<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"

    />

- &GT;页脚

<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());

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

如果你有任何想法:&#39;(!

谢谢

记录:

01-06 15:03:39.845 23899-23899 / com.example.thibault.project_mobile E / AndroidRuntime:java.lang.NullPointerException:尝试调用虚方法&#39; void android.widget.TextView.setText( java.lang.CharSequence中)&#39;在空对象引用上

01-06 15:03:39.845 23899-23899 / com.example.thibault.project_mobile E / AndroidRuntime:at 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:at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:451)

1 个答案:

答案 0 :(得分:0)

  

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

因为TextView textView1 ID仅在listanswer_home.xml布局中可用,而不在test_edit_text.xmlif (childPosition < getChildrenCount(groupPosition) - 1)条件为false重新调整{{}来自test_edit_text.xmlgetChildView的布局返回convertView.findViewById(R.id.textView1);

在调用null null方法之前添加setText检查为:

TextView

或在 if(text !=null){ text.setText(answer.getAnswer()); } 中添加TextView textView1