How to maintain the context of the main activity in all the elements of the ViewPager?

0

I had my application in a single layout, in order to improve it I am trying to implement a main layout with a ViewPager and through it access the different layout in which I divided the app. When I had everything in one layout, it did not present any problems. Method onCreate without using ViewPager:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_valor_comercial);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        th = (TabHost) findViewById(R.id.tabhost);
        th.setup();
        manager = new DBManager(this);
        dialog = new AlertDialog.Builder(this);

        TabSpec specs = th.newTabSpec("Valor Comercial");
        specs.setIndicator("Valor Comercial");
        specs.setContent(R.id.tab1);
        th.addTab(specs);
        sMarcas = (Spinner) findViewById(R.id.sMarca);
        sSubMarcas = (Spinner) findViewById(R.id.sSubMarca);
        sAnios = (Spinner) findViewById(R.id.sModelo);
        sModelo = (Spinner) findViewById(R.id.sTipo);
        tValorVenta = (TextView) findViewById(R.id.tValorVenta);
        tValorCompra = (TextView) findViewById(R.id.tValorCompra);
        tMoneda = (TextView) findViewById(R.id.tMoneda);
        tPeriodo = (TextView) findViewById(R.id.tPeriodo);
        tVcTipo = (TextView) findViewById(R.id.tVCTipo);
        equipos = new String[0];
        equipoCheck= new boolean[0];

        buscarMarcas();
}

Now that I am using ViewPager, my app does not run (it closes when it is installed). Apparently the problem arises when I call the searchFrames () method . Method onCreate using ViewPager:

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

    setContentView(R.layout.activity_main);

    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(new CustomPagerAdapter(this));


    manager = new DBManager(this);
    dialog = new AlertDialog.Builder(this);

    sMarcas = (Spinner) findViewById(R.id.sMarca);
    sSubMarcas = (Spinner) findViewById(R.id.sSubMarca);
    sAnios = (Spinner) findViewById(R.id.sModelo);
    sModelo = (Spinner) findViewById(R.id.sTipo);
    tValorVenta = (TextView) findViewById(R.id.tValorVenta);
    tValorCompra = (TextView) findViewById(R.id.tValorCompra);
    tMoneda = (TextView) findViewById(R.id.tMoneda);
    tPeriodo = (TextView) findViewById(R.id.tPeriodo);
    tVcTipo = (TextView) findViewById(R.id.tVCTipo);
    equipos = new String[0];
    equipoCheck= new boolean[0];

    buscarMarcas();



}

SearchMarks method ():

public void buscarMarcas(){
    ArrayAdapter<Marca> adapterMarca = new ArrayAdapter<Marca>(this, android.R.layout.simple_spinner_dropdown_item);
    Marca marca = new Marca();
    marca.setCdMarca(0L);
    marca.setDescMarca("");
    adapterMarca.add(marca);
    Cursor rs = manager.selectMarcas();
    while(rs.moveToNext()){
        marca = new Marca();
        marca.setCdMarca(rs.getLong(0));
        marca.setDescMarca(rs.getString(1));
        adapterMarca.add(marca);
    }
    this.sMarcas.setAdapter(adapterMarca);
}

How can I solve this problem? Thanks for the support ...

    
asked by Luis-Ortiz 11.07.2018 в 19:54
source

0 answers