Skip to content Skip to sidebar Skip to footer

Android Viewpager Using A Single Fragment On Multiple Pages

I'm having a bit of trouble having the ViewPager displaying and running properly with a single fragment class. So a bit of background. The Activity itself is supposed to allow user

Solution 1:

It looks like the R.id.tv_question_title, R.id.tv_question_title and R.id.tv_question_title views are part of your R.layout.fragment_question layout. You should be referencing those views through your SurveyTakerFragment instance using getView(), not through the Activity via getActivity(). You are also duplicating effort in onActivityCreated() and setup().

Replace your onActivityCreated() and setup() implementations with this:

@OverridepublicvoidonActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setup();
}

voidsetup(){
    View view = getView();
    tv_question_title = (TextView) view.findViewById(R.id.tv_question_title);
    et_sms_response = (EditText) view.findViewById(R.id.et_sms_response);
    btn_submit_response = (Button) view.findViewById(R.id.btn_submit_response);
    tv_question_title.setTypeface(typeface);
    tv_question_title.setText("Question: " + index);
    //TODO: Set question title.//TODO: Pre-fill previous answer if any.
    btn_submit_response.setOnClickListener(newOnClickListener() {
        @OverridepublicvoidonClick(View v) {
            String response = et_sms_response.getText().toString();
            //TODO: Submit response.
        }
    });
}

Post a Comment for "Android Viewpager Using A Single Fragment On Multiple Pages"