2

So I have this code: android studio marks casts as redundant.

But if I remove casts I will get compile error:

Error:(42, 29) Type mismatch: cannot convert from View to Button

Error:Execution failed for task ':app:transformJackWithJackForDebug'. com.android.build.api.transform.TransformException: com.android.builder.core.JackToolchain$ToolchainException: Jack compilation exception

What am I doing wrong? Android Studio v2.3.3

Community
  • 1
  • 1
Daniil S
  • 21
  • 3
  • Default way is to keep casts which was required until compileSdkVersion 26. If you set compileSdkVersion to 26 you don't need to use casting. – Thracian Oct 18 '17 at 15:36
  • @FatihOzcan I have this issue with compileSdkVersion 26. – Daniil S Oct 18 '17 at 15:43
  • 1
    Try cleaning and rebuilding your project, it may help. – Thracian Oct 18 '17 at 15:44
  • As pointed by others this is a problem because the sdk 26, please look at this https://stackoverflow.com/questions/45577746/how-to-use-public-t-extends-view-t-findviewbyid-int-id-in-activity-android – cutiko Oct 18 '17 at 15:52
  • @FatihOzcan this actually helps. Thank you! – Daniil S Oct 18 '17 at 15:53

3 Answers3

1

The declaration of View#findViewById on Android <= 25 is

View findViewById(int resId);

Requiring explicit cast for usage.

While the declaration on Android >= 26

<T> T findViewById(int resId);

It does the unsafe cast for you and returns the expected value for the assignment.

If you compile level is 26 you will be using the new method.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
0

Probably the elem with Id buttonLoad is not a Button but a View.

Provide the code of the layout if it isn't the solution

bra_racing
  • 622
  • 1
  • 8
  • 32
0

So the solution was just cleaning up and rebuilding the project. Thanks to Fatih Ozcan

Daniil S
  • 21
  • 3