Spinner returning Null and Void

Asked

Viewed 56 times

0

My spinner is returning null, probably at the time of passing to the string there is something wrong and there is an error in these lines :

com.example.thiago.myapplication.Activity_addinf.onCreate(Activity_addinf.java: 128)
Caused by: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner.

My code :

   public class Activity_addinf extends AppCompatActivity implements IHttpConnection, AdapterView.OnItemSelectedListener {


   catliv = (Spinner) findViewById(R.id.catergorialiv);

    List<String> list = new ArrayList<String>();
    list.add("Romance");
    list.add("Direito");
    list.add("Didatico");
    list.add("Informática");
    list.add("Ficção Cientifíca");
    list.add("Infantil");
    list.add("Drama");
    list.add("Contos");

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    catliv.setAdapter(dataAdapter);





   btnadd1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {

           // mCatliv = parent.getItemAtPosition(position).toString();



            if (mCatliv.equals("Romance")) {
                postCatLivro("http://192.168.1.207/api/v2/bookdemo/_table/cad_categorias?fields=id_cat&filter=tx_cat%3Dromance", 2);
            }
            else if (mCatliv.equals("Direito")) {
                postCatLivro("http://192.168.1.207/api/v2/bookdemo/_table/cad_categorias?fields=id_cat&filter=tx_cat%3DDireito", 3);
            }
            else if (mCatliv.equals("Didatico")) {
                postCatLivro("http://192.168.1.207/api/v2/bookdemo/_table/cad_categorias?fields=id_cat&filter=tx_cat%3DDidatico", 4);
            }
            else if (mCatliv.equals("Informática")) {
                postCatLivro("http://192.168.1.207/api/v2/bookdemo/_table/cad_categorias?fields=id_cat&filter=tx_cat%3DInform%C3%A1tica", 5);
            }
            else if (mCatliv.equals("Ficção Cientifíca")) {
                postCatLivro("http://192.168.1.207/api/v2/bookdemo/_table/cad_categorias?fields=id_cat&filter=tx_cat%3DFic%C3%A7%C3%A3o%20Cientif%C3%ADca", 6);
            }
            else if (mCatliv.equals("Infantil")) {
                postCatLivro("http://192.168.1.207/api/v2/bookdemo/_table/cad_categorias?fields=id_cat&filter=tx_cat%3DInfantil", 7);
            }
            else if (mCatliv.equals("Drama")) {
                postCatLivro("http://192.168.1.207/api/v2/bookdemo/_table/cad_categorias?fields=id_cat&filter=tx_cat%3DDrama", 8);
            }
            else if (mCatliv.equals("Contos")) {
                postCatLivro("http://192.168.1.207/api/v2/bookdemo/_table/cad_categorias?fields=id_cat&filter=tx_cat%3DContos", 9);
            }


            else {
                return;
            }



  @Override
 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

}

@Override
  public void onNothingSelected(AdapterView<?> parent) {

}

1 answer

0

The mistake says it all. You are trying to set a setOnItemClickListener in Spinner somewhere in the code, but Spinner does not implement this interface. As the documentation, you have to set the setOnItemSelectedListener

Browser other questions tagged

You are not signed in. Login or sign up in order to post.