2016-01-06 22 views
0

我有一個問題,我正在做一個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)

+0

你是否得到一些例外,如果是的請發佈你的logcat。 –

+0

帶有'textView1'的TextView在'listanswer_home'佈局或'test_edit_text'裏面? –

+0

我添加xml文件,兩秒 –

回答

0

的錯誤是在適配器:text.setText(答案。的getAnswer());

因爲TextViewtextView1 ID只在listanswer_home.xml佈局不test_edit_text.xml如果if (childPosition < getChildrenCount(groupPosition) - 1)條件falsegetChildViewconvertView.findViewById(R.id.textView1);回報null重新調整佈局test_edit_text.xml可用。

要麼調用的TextViewsetText方法前加null檢查:

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

textView1listanswer_home.xml添加TextView

+0

哦,完美的工作:D。我現在會試着去理解它。 謝謝! –

+0

但我真的不明白爲什麼如果條件是錯誤的,那麼我們去後行convertView.findViewById(R.id.textView1)。 因爲如果它是頁腳,我們會進入其他頁面,我們從來沒有看到這一行? –

相關問題