简体   繁体   中英

error: incompatible types: inferred type does not conform to upper bound(s) inferred: INT#1 upper bound(s):

I really don't know how to solve this problem (error: incompatible types: inferred type does not conform to upper bound(s) inferred: INT#1 upper bound(s): Giocatore[],Parcelable where INT#1 is an intersection type: INT#1 extends Giocatore[],Parcelable).

It shows the error on the line " Giocatore[] gio = intent.getParcelableExtra("giocatori");".

public class Giocatore implements Parcelable {

    private String nome;
    private String ruolo;
    private double valore;

    public Giocatore(String nome, String ruolo, double valore) {
        setNome(nome);
        setRuolo(ruolo);
        setValore(valore);
    }
    //getters and setters
    protected Giocatore(Parcel in) {
        nome = in.readString();
        ruolo = in.readString();
        valore = in.readDouble();
    }

    public static final Creator<Giocatore> CREATOR = new Creator<Giocatore>() {
        @Override
        public Giocatore createFromParcel(Parcel in) {
            return new Giocatore(in);
        }

        @Override
        public Giocatore[] newArray(int size) {
            return new Giocatore[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(nome);
        dest.writeString(ruolo);
        dest.writeDouble(valore);
    }
}

Class "Squadra"

public class Squadra {

    private int componenti;
    private String coloreMaglia;
    private Giocatore[] squadra;

    public Squadra() {
        this.componenti = 5;
        this.coloreMaglia = null;
        squadra = new Giocatore[5];
    }

    public Giocatore[] getSquadra() {
        return this.squadra;
    }
}

Class "SecondaPagina"

public class SecondaPagina extends AppCompatActivity  implements AdapterView.OnItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page2);

        final Giocatore[] giocatori = new Giocatore[10];

        Button next = findViewById(R.id.crea);
        Button procedi = findViewById(R.id.procedi);
        procedi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
               //some code
               int pos = aggiungi(giocatori, new Giocatore(n, r, v)); //function "aggiungi" add an object "Giocatore" to the array "giocatori" and return the position where the object has been added (100% works)
               //some code
             }
        });
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(SecondaPagina.this, TerzaPagina.class);
                intent.putExtra("giocatori", giocatori);
                startActivity(intent);
            }
        });
    }
}

public class TerzaPagina extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page3);

        Intent intent = getIntent();
        Giocatore[] gio = intent.getParcelableExtra("giocatori");
    }
}

error: incompatible types: inferred type does not conform to upper bound(s) inferred: INT#1 upper bound(s): Giocatore[],Parcelable where INT#1 is an intersection type: INT#1 extends Giocatore[],Parcelable

It shows the error on the line " Giocatore[] gio = intent.getParcelableExtra("giocatori");".

Please help me, thanks.

Try to use:

(Giocatore[]) intent.getParcelableArrayExtra("giocatori");

instead of

intent.getParcelableExtra(...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM