Fill Spinner and ListView - Fragment

0

I'm developing an application with Fragment. I have two fragments, where in one there is a ListView and the other a Spinner.

The ListView and Spinner is populated with data that comes from the database.

During debugging I realized that when I get into the setAdapter: listView.setAdapter (adapter) or spinner.setAdapter (adapter) method an error is generated and enters the Catch (Exception) block.

The error you give is as follows:

Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

I have followed the debugging, the data is coming from the database, everything is correct for this line of catch (Execption).

In both cases (fill in Spinner and ListView) the error is indeed.

Follow the OnCreateView code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View mae = inflater.inflate(R.layout.fragment_usar_produto, container, false);
    spProduto = (Spinner)mae.findViewById(R.id.spProduto);
    npQuantidade = (NumberPicker)mae.findViewById(R.id.npQuantidade);

    npQuantidade.setMinValue(0);
    npQuantidade.setMaxValue(1000);
    npQuantidade.setValue(30);
    npQuantidade.setWrapSelectorWheel(false);

    preencherSpinner();


    btAtualizar = (Button)mae.findViewById(R.id.btAtualizar);
    btAtualizar.setOnClickListener(atualizar);


    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_usar_produto, container, false);
}

Now the method code to populate Spinner

   public void preencherSpinner()
{
    StringBuilder sql = new StringBuilder();
    sql.append("SELECT * FROM validade ORDER BY produto");

    Validade val = new Validade();

    cursorProdutos = val.listarProduto(getActivity().getBaseContext());

    ArrayList<String> listaProdutos = new ArrayList<String>();

    if (cursorProdutos.moveToFirst()) {
        do {
            listaProdutos.add(cursorProdutos.getString(1));
        } while (cursorProdutos.moveToNext());
    }

    try
    {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_list_item_1,listaProdutos);

        spProduto.setAdapter(adapter);
    }
    catch (Exception ex)
    {
        Log.d("exceção", ex.getMessage());
    }


}
    
asked by anonymous 20.03.2017 / 23:26

0 answers