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
14
votes
3 answers
findViewById retrurns wrong element?
I can't understand why findViewById returns the wrong element, here is the class:
public class EventDetailsFragment extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
…

jonepatr
- 7,769
- 7
- 30
- 53
14
votes
2 answers
Efficiency of findViewById
Probably most Android devs know that findViewById is not a cheap operation. Another thing that most of us know, is that you can boost the performance by using the smallest sub-tree of the view hierarchy to find views by their id,…

overbet13
- 1,654
- 1
- 20
- 36
14
votes
4 answers
findViewById() returns null for Views in a Dialog
The problem is, no matter where or how I call for this layout's components, they always return null.
setView(inflater.inflate(R.layout.search_layout, null))
This works fine. It displays the layout inside the Dialog, yet, the children are always…

Fer
- 460
- 2
- 4
- 17
11
votes
4 answers
Android O Preview findViewById compile error
I tried to test the Android O Developer Preview second phase.
After the project was created, I just clicked build and run but I didn't have any success.
Android default generated code below:
Toolbar toolbar = (Toolbar)…

Kihu Kwon
- 111
- 1
- 4
11
votes
3 answers
Swift: get UI objects by ID?
in iOS, is it possible to assign a string ID to UI objects and then retrieve them in the code by that ID?
I am looking for something similar to the Android's findViewById(id)

Daniele B
- 19,801
- 29
- 115
- 173
11
votes
9 answers
Why does my Android app crash with a NullPointerException when initializing a variable with findViewById(R.id.******) at the beginning of the class?
This code, with the block at the top commented, runs successfully:
public class MainActivity extends AppCompatActivity {
/*
EditText username = (EditText)findViewById(R.id.editText_Username);
EditText password =…

000
- 405
- 5
- 20
10
votes
1 answer
findViewById in a custom View to find child view
I defined a view, it extends RelativeLayout, I want to extract its children whose id are specified in layout xml file.
In HeaderView.java:
1 package com.example.test;
2
3 public class HeaderView extends RelativeLayout {
4 …

srain
- 8,944
- 6
- 30
- 42
9
votes
5 answers
Casting to Button is redundant - Why?
I've just came across this interesting message from the compiler and I do not know why is it happening. Here is the case
Example 1.
Button test = (Button) findViewById(R.id.someButtonId);
test.setOnClickListener(this);
Example…

sandalone
- 41,141
- 63
- 222
- 338
9
votes
5 answers
Cannot resolve findViewById in fragment
i am trying to get id from fragment xml so here is the code
the error is
" Cannot resolve the method findViewById "
Is there anyway of which I can use findViewById in Fragment ?
public class Slide_Teacher_Add extends Fragment implements…

M Andre Juliansyah
- 117
- 1
- 2
- 14
9
votes
3 answers
findViewById inside a Static Method
I have this static method:
public static void displayLevelUp(int level, Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout =…

TheLettuceMaster
- 15,594
- 48
- 153
- 259
9
votes
3 answers
Android Activity findviewbyid() is null
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
public class FileExplorerActivity extends Activity
{
public static final String TAG="ricky";
Button button;
protected void…

Anjanu
- 623
- 2
- 8
- 19
8
votes
1 answer
Android fragment - findViewById returns null
I have a problem that seems to be quite common on the internet : I have created an activity with just a fragment. This is the generated code:
@Override
protected void onCreate(Bundle savedInstanceState) {
…

Charline
- 283
- 1
- 2
- 13
7
votes
1 answer
Kotlin with Android: Base classes and Kotlin Android Extensions
So i have used Kotlin Android Extensions and i find it very easy to use and well worth it. No more findViewById or Butterknife.Bind(...). I have found no issue with it all all except for one situation.
In a base class, for example, BaseActivity,…

johnny_crq
- 4,291
- 5
- 39
- 64
7
votes
3 answers
How does findViewById work?
package com.example.dell.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class…

q126y
- 1,589
- 4
- 18
- 50
7
votes
2 answers
How do I "findViewById" with fragments?
I recently moved my project to Android Studio from Eclipse. I am trying to get the email functionality working, however I receive errors by "findViewById". I also receive errors by "Toast.makeText". Could you please assist on both errors. The code…

Yusuf
- 71
- 1
- 1
- 4