亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

lambda 表達式中的錯誤返回類型:無法轉換為布爾值

lambda 表達式中的錯誤返回類型:無法轉換為布爾值

江戶川亂折騰 2022-06-15 14:40:22
我需要通過觸摸按鈕來更改 shapeType。我嘗試了這種方法,但出現錯誤,提示 shapeType 無法轉換為布爾值。代碼:public class MainActivity extends AppCompatActivity {private ArFragment arFragment;private enum ShapeType {    GIRAFFE,    HIPPO,    ELEPHANT}private ShapeType shapeType = ShapeType.GIRAFFE;@SuppressLint("ClickableViewAccessibility")@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);    Button giraffe = findViewById(R.id.giraffebutt);    Button hippo = findViewById(R.id.hippobutt);    Button elephant = findViewById(R.id.elephantbutt);    giraffe.setOnTouchListener(view -> shapeType = ShapeType.GIRAFFE);    hippo.setOnTouchListener(view ->shapeType = ShapeType.HIPPO);    elephant.setOnTouchListener(view ->shapeType = ShapeType.ELEPHANT);  }}錯誤:Bad return type in lambda expression: ShapeType cannot be converted to boolean
查看完整描述

2 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊

您的onTouch()方法現在期望返回一個布爾值,您沒有返回任何東西,如下更改您的 lambda。


giraffe.setOnTouchListener(

(view, motionEvent) -> {

    shapeType = ShapeType.GIRAFFE;

    return false;

});


查看完整回答
反對 回復 2022-06-15
?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

ButtonsetOnTouchListener提供一個Viewand MotionEvent,并期望一個boolean返回類型。


giraffe.setOnTouchListener((view, event) -> {

    shapeType = ShapeType.GIRAFFE);

    return true; // return true if event is consumed, false otherwise

});


查看完整回答
反對 回復 2022-06-15
  • 2 回答
  • 0 關注
  • 565 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號