Search code examples
javaandroid-studioandroid-checkboxtype-parameter

Android Studio Check Box


I'm looking for help with this error:

error: incompatible types: no unique maximal instance exists for type variable T with upper bounds CheckBox,View where T is a type-variable: T extends View declared in method findViewById(int)

...which I get when running this code:

public class CheckBox extends AppCompatActivity {

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

        CheckBox checkbox1 = new CheckBox();
        checkbox1 = findViewById(R.id.bx1);
    }
}

Solution

  • You need to cast the checkbox because the returned form findViewByIdis generic

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);
    
        CheckBox checkbox1 = (Checkbox) findViewById(R.id.bx1);
    }