在PagerAdapter中将值设置为TextView时出现Nullpointer异常

时间:2017-03-01 07:43:32

标签: android android-viewpager android-pageradapter

我有一个MainActivity,我在布局中对视图页面适配器进行了decelared我有4个布局,这些布局正在迭代并在viewpager适配器的下一个按钮上显示布局我无法将texttext设置为任何textview或编辑我在布局可以任何人帮助我,当我设置文本时,我得到一个Null指针异常。

我的代码段如下

public class Submit_form extends AppCompatActivity {

private ViewPager viewPager;
private MyViewPagerAdapter myViewPagerAdapter;
private LinearLayout dotsLayout;
private TextView[] dots;
private int[] layouts;

private Button btnSkip, btnNext;

String[] board_size = {"Select Your Size", "8 x 6 ft", "10 x 8 ft", "12 x 8 ft", "12 x 10 ft", "16 x 10 ft", "16 x 12 ft", "20 x 10 ft", "22 x 10 ft", "24 x 12 ft", "30 x 10 ft", "40 x 10 ft", "50 x 10 ft", "60 x 10 ft", "70 x 10 ft", "80 x 10 ft"};

String nameInput1,nameInput2,nameInput3,nameInput4,nameInput5,nameInput6,nameInput7, nameInput8,nameInput9,nameInput10,nameInput11,nameInput12,nameInput13,nameInput14,nameInput15,nameInput16,nameInput17,nameInput18,nameInput19,nameInput20,nameInput21,nameInput22,nameInput23, nameInput24, nameInput25, nameInput26, nameInput27, nameInput28, nameInput29, nameInput30, nameInput31, nameInput32, nameInput33,nameInput34,nameInput35,nameInput36,nameInput37,nameInput38,nameInput39;


Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://docs.google.com/a/aishwaryaled.com/forms/d/e/")
        .build();
final QuestionsSpreadsheetWebService spreadsheetWebService = retrofit.create(QuestionsSpreadsheetWebService.class);


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

    // Making notification bar transparent
    if (Build.VERSION.SDK_INT >= 21) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }

    setContentView(R.layout.activity_submit_form);



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





    // layouts of all welcome sliders
    // add few more layouts if you want
    layouts = new int[]{
            R.layout.welcome_slide1,
            R.layout.welcome_slide2,
            R.layout.welcome_slide3,
            R.layout.welcome_slide4};

    // adding bottom dots
    addBottomDots(0);

    // making notification bar transparent
    changeStatusBarColor();

    myViewPagerAdapter = new MyViewPagerAdapter(getApplicationContext());
    viewPager.setAdapter(myViewPagerAdapter);
    viewPager.addOnPageChangeListener(viewPagerPageChangeListener);





    btnSkip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(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);
            }                  

MyViewPagerAdapter类

    public class MyViewPagerAdapter extends PagerAdapter {

    private LayoutInflater layoutInflater;
    private Context mContext;
    TextView textView37;
    EditText edtxt12;

    public MyViewPagerAdapter(Context context) {
        this.mContext = context;
    }

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

        Log.e("Position:::::::::",Integer.toString(layouts[position])+":::::Child Count::::::"+container.getChildCount());
        View view = layoutInflater.inflate(layouts[position], null);
        Log.e("View::::::::",view.toString());

        textView37=(TextView)view.findViewById(R.id.textView37);

  **textView37.setText("0");**//getting error Null pointer exception here 

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

2 个答案:

答案 0 :(得分:0)

在扩充XML文件时,是否可以尝试精确定位视图组。

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

答案 1 :(得分:0)

检查您的layouts[position]包含R.id.textView37 TextView

我认为您的子视图的XML数组应该被错误地更改,或者您忘记在TextView中添加layouts[position]特定的位置布局XML。