我有一个问题,我正在做一个android应用程序,我想做一个问题列表,当我们点击一个问题时,它会滚动回答列表(在一个片段中)。 我做了一个自定义适配器,我想在每个问题的答案列表(我想添加一个edittext来添加一个anwser)之后添加一个页脚。Error BaseExpandableListAdapter textview null
当我滚动答案一个问题,它与页脚运作良好,但是当我尝试滚动第二次相同的列表,他说我说的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;
}
}
- >片段的代码:
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/c om.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。示例。 thibault.project_mobile E/AndroidRuntime:在android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:451)
你是否得到一些例外,如果是的请发布你的logcat。 –
带有'textView1'的TextView在'listanswer_home'布局或'test_edit_text'里面? –
我添加xml文件,两秒 –