Null pointer Exception - findViewById()

后端 未结 10 2686
情歌与酒
情歌与酒 2020-11-21 04:16

Can anyone help me to find out what can be the issue with this program. In the onCreate() method the findViewById() returns null for all ids and th

10条回答
  •  终归单人心
    2020-11-21 05:10

    The views you're trying to get are not defined in your activity_main layout. You need to programmatically inflate the views you're trying to add to the pager.-

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        LinearLayout l = null;
    
        if (position == 0) {
            l = (LinearLayout) View.inflate(this, R.layout.activity_first, null);
        }
        if (position == 1) {
            l = (LinearLayout) View.inflate(this, R.layout.activity_second, null);
        }
        if (position == 2) {
            l = (LinearLayout) View.inflate(this, R.layout.activity_third, null);
        }
    
        collection.addView(l, position);
        return l;
    }
    

提交回复
热议问题