The Operator + is Undefined for the argument type(s) Charsequence, int

Asked

Viewed 333 times

1

I’m trying to concatenate a text into CharSequence with int, but java warns that it is not possible to do this conversion by saying:

The Operator + is Undefined for the argument type(s) Charsequence, int

I already tried to remove the casting of CharSequence, in addition to "give" a .toString() in everything, but the app doesn’t start when I do it.

package br.edu.ifgoiano.cadastrodeprodutos;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class Cadastrar extends Activity {
    Principal principal = new Principal();
/*  private int cont = 0;
    private int varAux;*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cadastrar);
    }

    public void tirarFoto(View view) {
        Intent tiraFoto = new Intent("android.media.action.IMAGE_CAPTURE");
        startActivityForResult(tiraFoto, 0);
    }
    **public void qntProd() {
        int cont = 0;
        if (cont == 0) {
            principal.quanti.setText((CharSequence) "Você não possui produtos cadastrados.");
        } else {
            principal.quanti.setText("Você possui " + cont +" produtos cadastrados.");
        }**
    }
    /*public int qntProdCadastrados() {
            if(cont == 0) {
                varAux =  cont;
            }else if(cont > 0){
                varAux = cont;
            }
        cont++;
        return varAux;
    }*/
    public void enviar(View view) {
        Toast.makeText(this, "O cadastro foi realizado com sucesso", Toast.LENGTH_LONG).show(); 
        Intent telaInicial = new Intent(this, Principal.class);
        startActivity(telaInicial);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(data != null) {
            Bundle bundle = data.getExtras();
            if(bundle != null) {
                Bitmap img = (Bitmap) bundle.get("data");
                ImageView imagem = (ImageView) findViewById(R.id.imagem);
                imagem.setImageBitmap(img);
            }
        }
    }
}
  • 1

    What is quanti? Why are you wearing (CharSequence)?

  • Pardoning... quanti is the Textview that will write on the screen. I am using Charsequence because the TextView uses a string of characters.

1 answer

2


I believe that this is the only way to work since the String be of a subtype of CharSequence:

principal.quanti.setText("Você possui " + cont + " produtos cadastrados.")

If you really need to, you can:

principal.quanti.setText((CharSequence)("Você possui " + cont + " produtos cadastrados."))

I put in the Github for future reference.

  • I already tried to do both cases, however the app does not run, giving the error Unfurtunalety, CadastroDeProdutos has stopped. You’d rather I put in the whole code ?

  • But it may have stopped for many reasons. If you take it out it works normal? In fact this mistake should be caught in the compilation, so one thing has nothing to do with another.

  • Without it it works properly.

  • but this code is not even running, it enters the if, doesn’t make sense.

  • This code is to say the amount of "registered" products in the system. I will put the full code.

Browser other questions tagged

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