【问题标题】:error: incompatible types: inferred type does not conform to upper bound(s) inferred: INT#1 upper bound(s):错误:不兼容的类型:推断的类型不符合推断的上限:INT#1 上限:
【发布时间】:2020-05-04 08:57:52
【问题描述】:

我真的不知道如何解决这个问题(错误:不兼容的类型:推断类型不符合上限推断:INT#1 上限:Giocatore[],Parcelable where INT# 1 是交集类型:INT#1 extends Giocatore[],Parcelable)。

它在“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);
    }
}

类“中队”

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;
    }
}

类“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");
    }
}

错误:不兼容的类型:推断的类型不符合上限 推断:INT#1 上限:Giocatore[],Parcelable 其中 INT#1 是交集类型: INT#1 扩展 Giocatore[],Parcelable

它在“Giocatore[] gio = intent.getParcelableExtra("giocatori");”这一行显示错误。

请帮帮我,谢谢。

【问题讨论】:

    标签: java android compiler-errors incompatibletypeerror inferred-type


    【解决方案1】:

    尝试使用:

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

    而不是

    intent.getParcelableExtra(...);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 2019-04-25
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 2017-11-03
      相关资源
      最近更新 更多