No need to cast the result of findViewById?
Starting with API 26, findViewById
uses inference for its return type, so you no longer have to cast.
Old definition:
View findViewById(int id)
New definition:
<T extends View> T findViewById(int id)
So if your compileSdk
is at least 26, it means that you can make use of this :)
Android O casting to findViewById not needed anymore?
The method signature changed as you noticed and now it looks like:
public <T extends View> T findViewById(int id);
compared to the old (pre SDK 26) one:
public View findViewById(int id);
so as long as you use SDK 26 (or newer) to compile your project you can safely remove the casting from your code as you will be using new findViewById()
which no longer requires it.
so having a lower minSdk than 26 will not cause an issue ?
No, neither minSdk
nor targetSdk
really matter. What matters is compileSdk
which must be 26
or higher.
Casting result of findViewById is redundant
Before API level 26, the method findViewById
returned the reference of View
class. So you needed to cast it.
//old signature
public View findViewById(int id){
//
}
But starting from API level 26, it has been updated and it returns subclass of View
using template so that you can assign the returned reference without casting.
//new signature
public <T extends View > T findViewById(int id){
//
}
The example which you referred used the older API level while building the project, so you can see the casting there. It was compulsory earlier but is not necessary now. So you are getting the warning.
Android: a findViewById() method that returns values we don't need to cast
This works fine in my test project. No compiler error:
Cast result findViewById android method
The method findViewById()
returns an instance of the class that is actually used to define that view in your XML file. The method signature returns a View
to make it generic and usable for all classes that inherit for View
.
You need to cast the returned value to the class that your variable is defined when you use it like:
ImageView image = (ImageView) findViewById(R.id.image);
Java won't cast that implicitly for you.
You could leave it as:
View image = findViewById(R.id.image);
but you wouldn't be able to use the methods, etc. defined on ImageView
class.
Casting 'findViewById(R.id.)' to 'Button' is redundant
It does not affect your code. This is a new feature of Android studio that directly maps the widget from your XML to Java code. Your app may crash for any other reason
getting Report unnecessary cast expressions MainActivity.onCreate
There is a similar question with an explanation - No need to cast the result of findViewById?
Solution
In your code no need to cast the result of findViewById API - see below
nDrawerLayout = findViewById(R.id.drawerlayout);
findViewById not accepting type argument even with compileSdkVersion 30
I think you're confused with the syntax of java.
Here public <T extends View> T findViewById(@ResId int id)
means :
return value cast to T, T is resolved with the left side of the assignment declaration before the equals sign. In the following example
Edittext x = view.findViewById(R.id.abc)
So here T get assigned as Edittext as Edittext is on the left side of the assignment declaration which then returns edittext as a view. Then you can call x to getstring.
Hope this makes it clearer
How do I refactor findViewById without class cast exception?
Try the library Butterknife, I think its what you are looking for. It will make your code cleaner :)
The example from the website is:
class ExampleActivity extends Activity {
@InjectView(R.id.title) TextView title;
@InjectView(R.id.subtitle) TextView subtitle;
@InjectView(R.id.footer) TextView footer;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.inject(this);
// TODO Use "injected" views...
}
}
Related Topics
Android Plugin Install Problems in Eclipse
What Is Exactly a "Clock Tick" in the Context of Android CPU Usage
Android Textwatcher.Aftertextchanged VS Textwatcher.Ontextchanged
Gradle - Library Duplicates in Dependencies
"No Such File or Directory" Trying to Execute Linux Binary on Android Device
Android Ndk: Getting the Backtrace
What Kind of Font Files Do I Need for Modern Browsers, Android and iOS
How to Change the Color of Header Bar and Address Bar in Newest Chrome Version on Lollipop
How to Autoplay HTML5 Mp4 Video on Android
Html: Why Does Android Browser Show "Go" Instead of "Next" in Keyboard
How Much Support Is There for -Webkit-Overflow-Scrolling:Touch
How to Take a Screenshot of a Current Activity and Then Share It
How to Set Android Layout to Support All Screen Sizes
Accessing Sharedpreferences Through Static Methods