findViewById(int id) is a method of the View and Activity classes. This method will take a resource Id usually in the form of R.id.mView and will return to you a View object that is a reference to that View. Note that the returned object will nearly always need to be cast to the correct type of View before you can start interacting with it. If a View by the given ID does not exist within the current activity or parent View then you will receive null.
Questions tagged [findviewbyid]
563 questions
1185
votes
37 answers
findViewById in Fragment
I am trying to create an ImageView in a Fragment which will refer to the ImageView element which I have created in the XML for the Fragment. However, the findViewById method only works if I extend an Activity class. Is there anyway of which I can…

simplified.
- 11,977
- 5
- 17
- 8
212
votes
3 answers
How can I assign an ID to a view programmatically?
In an XML file, we can assign an ID to a view like android:id="@+id/something" and then call findViewById(), but when creating a view programmatically, how do I assign an ID?
I think setId() is not the same as default assignment. setId() is…

DuyguK
- 4,250
- 9
- 33
- 46
156
votes
6 answers
No need to cast the result of findViewById?
Recently I found that AndroidStudio reminds me to remove some class cast. I remember that in the old time, we have to cast the result of findViewById, but now it's not necessary.
The result of findViewById is still View, so i want to know why we…

Eric Zhao
- 1,704
- 2
- 12
- 14
59
votes
12 answers
Null pointer Exception - findViewById()
Can anyone help me to find out what can be the issue with this program.
In the onCreate() method the findViewById() returns null for all ids and this causes a null pointer exception later. I can not figure out why the findViewById() can not find the…

user2629828
- 685
- 1
- 8
- 13
48
votes
4 answers
using findviewbyid in a class that does NOT extend Activity in android
I have a class that is currently extending Activity and I have methods like findViewById, ArrayAdapter etc.
I want to turn it into an independent class but all the above methods become undefined. What is the problem? Shouldn't importing the classes…

Namratha
- 16,630
- 27
- 90
- 125
43
votes
12 answers
How to use view binding in Android
I have been using findViewById and then ButterKnife to bind views. Recently, I came across this article and am not quite sure how to use it.
I tried doing it but it does not seem to work in Android Studio 3.4.2
val binding =…

siera
- 443
- 1
- 4
- 6
32
votes
6 answers
Kotlin Android View Binding: findViewById vs Butterknife vs Kotlin Android Extension
I'm trying to figure out the best way to do Android View Binding in Kotlin. It seems like there are a few of options out there:
findViewById
val button: Button by lazy { findViewById

triad
- 20,407
- 13
- 45
- 50
30
votes
9 answers
Can not resolve method 'findViewById(int)'
I'm having a trouble with findViewByid but I can't find where the problem is.
Here's my FirstFragment class code:
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import…

Ferhat
- 313
- 1
- 3
- 5
30
votes
4 answers
NoSuchFieldError on findViewById()
There's two android projects I want to merge, one is Main and the other is linked as Library.
But I have some troubles on this line:
Button modificarC;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
…

Mauricio Zárate
- 440
- 1
- 4
- 8
30
votes
1 answer
Access Fragment View from Activity's onCreate
I am in the process of making my first app for Android, and I have a Fragment that gets added to my Activity in the Activity's onCreate() method. The problem I am facing is that I am unable to find any of the views contained within the Fragment from…

Dan
- 1,198
- 4
- 17
- 34
27
votes
2 answers
Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
When my SplashActivity opens the LoginActivity my app crashes.
The following is my SplashActivity.java:
package com.example.android.appName;
import android.content.Intent;
import android.os.Bundle;
import…
user5404864
25
votes
5 answers
findViewById within fragment
Is there any way to find a view by id within the scope of a fragment? I'm using a series of fragments to render a specialized list. The fragments are loaded from a layout, so their widgets all have the same ids.
I suppose I can figure out a way to…

Peri Hartman
- 19,314
- 18
- 55
- 101
19
votes
1 answer
Class cast exception to same class on Android
I'm having a strange problem with ClassCastException on Android. One class cannot be casted to the same class:
java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.ClassCastException: com.example.model.BadeWrapper cannot be…

rwojcik
- 1,030
- 9
- 18
18
votes
7 answers
Android findViewById() in Custom View
I've written my custom View and i want to update some other views after interacting with my custom view.
Main Layout:

Jilberta
- 2,836
- 5
- 30
- 44
16
votes
3 answers
FindViewById where ID is dynamic string
I have a table of 40 squares and each has an ID. When I'm passing Bundle from another activity I need to extract Note, Color and ID from that bundle. And then the app will change/add text and change background of the square specified by extracted…
user1926837