LJulz LJulz - 9 months ago 92
Android Question

How setText in a Layout from a ViewPager

I try to set a Text in a Layout in a ViewPager, but if i do this there will be thrown a NullPointerException:

private ViewPager viewPager;
private ViewPagerAdapter viewPagerAdapter;
private LinearLayout dotsLayout;
private TextView[] dots;
private int[] layouts;
private Button btnNext;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);


setContentView(R.layout.activity_slider);



viewPager = (ViewPager) findViewById(R.id.view_pager);
dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
//btnSkip = (Button) findViewById(R.id.btn_skip);
btnNext = (Button) findViewById(R.id.btn_next);



layouts = new int[]{
R.layout.slide_header_bild_text,
R.layout.slide_header_bild_text_2,
R.layout.slide_01_03,
R.layout.slide_01_04,
R.layout.slide_01_05,
R.layout.slide_01_06,
R.layout.startquiz_layout};

//These two lines are the Problem
TextView t1 = (TextView) findViewByID(R.layout.header_text);
t1.setText("Test")

// adding bottom dots
addBottomDots(0);

viewPagerAdapter = new ViewPagerAdapter();
viewPager.setAdapter(viewPagerAdapter);
viewPager.addOnPageChangeListener(viewPagerPageChangeListener);



}

public void btnQuizStart(View v){
Intent intent = new Intent(this, Quiz.class);
this.startActivity(intent);
}



public void btnNextClick(View v)
{
// checking for last page
// if last page home screen will be launched
int current = getItem(1);
if (current < layouts.length) {
// move to next screen
viewPager.setCurrentItem(current);
} else {
launchHomeScreen();
}
}


ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {

@Override
public void onPageSelected(int position) {
addBottomDots(position);

// changing the next button text 'NEXT' / 'GOT IT'
if (position == layouts.length - 1) {
// last page. make button text to GOT IT
btnNext.setText(getString(R.string.start));

} else {
// still pages are left
btnNext.setText(getString(R.string.next));
}
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override
public void onPageScrollStateChanged(int arg0) {

}
};


private void addBottomDots(int currentPage) {
dots = new TextView[layouts.length];

dotsLayout.removeAllViews();
for (int i = 0; i < dots.length; i++) {
dots[i] = new TextView(this);
dots[i].setText(Html.fromHtml("&#8226;"));
dots[i].setTextSize(35);
dots[i].setTextColor(getResources().getColor(R.color.inactive_dots));
dotsLayout.addView(dots[i]);
}

if (dots.length > 0)
dots[currentPage].setTextColor(getResources().getColor(R.color.active_dots));
}


private int getItem(int i) {
return viewPager.getCurrentItem() + i;
}

private void launchHomeScreen() {
startActivity(new Intent(this, MainActivity.class));
finish();
}

public class ViewPagerAdapter extends PagerAdapter {
private LayoutInflater layoutInflater;


public ViewPagerAdapter() {

}

@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View view = layoutInflater.inflate(layouts[position], container, false);
container.addView(view);

return view;
}

@Override
public int getCount() {
return layouts.length;
}

@Override
public boolean isViewFromObject(View view, Object obj) {
return view == obj;
}


@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = (View) object;
container.removeView(view);
}
}


I've got the ViewPager which contains seven Pages.
I think the Layout wont be found, so the TextView is Null. I also read that I have to configure the instantiateItem method and to add there the TextView and the setter. Anybody can help?

Answer Source

oh this was also a failure but not which I searched for. I had the problem with the viewpager that i didn't got the layout and so the elements of the layout was null. I fixed the instantiateItem Method so any layout will be initiated specific:

public Object instantiateItem(ViewGroup container, int position) {
        layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);



        View one = layoutInflater.inflate(R.layout.slide_type_a, container, false);


        WebView header_1_a = (WebView) one.findViewById(R.id.header_slide_type_a_1);
        loadHTLMContentHeader(getString(R.string.Historie1_header),header_1_a);

        ImageView image_1_a = (ImageView) one.findViewById(R.id.image_slide_type_a_1);
        image_1_a.setImageResource(R.drawable.picture_kap01_01);
        WebView text_1_a = (WebView) one.findViewById(R.id.text_slide_type_a_1);
        loadHTLMContentText(getString(R.string.Historie1), text_1_a);


        View two = layoutInflater.inflate(R.layout.slide_type_a, container, false);

        View three = layoutInflater.inflate(R.layout.slide_type_a, container, false);
        View four = layoutInflater.inflate(R.layout.slide_type_b, container, false);

        View five = layoutInflater.inflate(R.layout.slide_type_b, container, false);

        View six = layoutInflater.inflate(R.layout.slide_type_b, container, false);

        View seven = layoutInflater.inflate(R.layout.slide_type_b, container, false);

        View eight = layoutInflater.inflate(R.layout.startquiz_layout, container, false);
        View viewarr[] = {one, two, three, four, five, six, seven, eight};
        container.addView(viewarr[position]);


        return viewarr[position];

    }

So after initiate the layout, i can get the xml element and change the content of it.