【问题标题】:can anyone help resolve the error here?任何人都可以帮助解决这里的错误吗?
【发布时间】:2015-07-05 01:44:54
【问题描述】:

ERROR 是“不兼容的类型”(如下注释)。请帮助解决问题。目前使用android studio 1.1.0。

 public void onClick(View view) {                            
            switch (view){
                case R.id.ETbutton:                                //ERROR: incompatible types
                    String gur = ET.getText().toString();           
                    if (gur.contentEquals("left")) {
                        tv.setGravity(Gravity.LEFT);                
                    } else if (gur.contentEquals("center")) {
                        tv.setGravity(Gravity.CENTER);
                    } else if (gur.contentEquals("right")) {
                        tv.setGravity(Gravity.RIGHT);
                    } else if (gur.contains("WTF")) {
                        tv.setText("Where's the Fridge?? ");
                        Random rand = new Random();
                        tv.setTextColor(Color.argb(40, rand.nextInt(265), rand.nextInt(266), rand.nextInt(267)));   //Tansparency, and rest colors
                        tv.setTextSize(rand.nextInt(100));                      
                    }
                    break;

                case R.id.toggleButton:                                           //ERROR: incompatible types
                    if(TB.isChecked()){
                        ET.setInputType(InputType.TYPE_CLASS_TEXT);                                 
                    }else{
                        ET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    }
                    break;
            }
        }

【问题讨论】:

  • R.id.ETbutton 应该是 view 类型

标签: android android-studio


【解决方案1】:

switch() 需要一个整数。您正在向它传递一个 View

这个

switch (view) // WRONG: switch doesn't want a View

必须是

switch(view.getId()) // CORRECT: The View id is an integer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 2022-06-13
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    • 2012-11-30
    • 2015-05-05
    相关资源
    最近更新 更多