GridLayout gridLayout = findViewById(; crashes my app

androidx gridlayout gridlayout
tutorial grid layout android studio
gridlayout android
dynamic grid layout android
android studio grid of buttons
android-gridlayout spacing
how to set a title for grid layout
what components should be used to produce grid in native android

Whenever I want to find my grid layout by ID my app crashes immediately upon starting. It is not a typo (I don't get any compiling errors).

It is the first thing I have declared in onCreate method so it doesn't crash because some other method would try to do something with the layout before the findViewById is called.

What could be the cause?

(I tried changing its ID, name... didn't work)

Part of the code:

public class MainActivity extends AppCompatActivity {

TextView countdownView, expressionView, scoreView, resultView, goView;
LinearLayout linearLayout;
GridLayout gridLayout;
CountDownTimer countDownTimer;
Random randomGenerator;
Button startGameButton;

protected void onCreate(Bundle savedInstanceState) {

    gridLayout = findViewById(;
    //other code here}

I had to change casting from this:

GridLayout gridLayout = findViewById(;

To this: gridLayout = findViewById(;

GridLayout gridLayout = findViewById(; crashes my , Whenever I want to find my grid layout by ID my app crashes immediately upon starting. It is not a findViewById(; I am a new android learner. We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand

findViewbyId is changed in android studio 3.*


findViewById() signature change All instances of the findViewById() method now return <T extends View> T instead of View. This change has the following implications:

This may result in existing code now having ambiguous return type, for example if there is both someMethod(View) and someMethod(TextView) that takes the result of a call to findViewById(). When using Java 8 source language, this requires an explicit cast to View when the return type is unconstrained (for example, assertNotNull(findViewById(...)).someViewMethod()). Overrides of non-final findViewById() methods (for example, Activity.findViewById()) will need their return type updated.

EditText message=super.findViewById(;

I am a new android learner. Directly trying codes of videos in my learning

Hands-on Approach to Android Development, For the same amount of work, we had to create so many files and it takes up so many space in the meory, it shouldn't be like this for this example, but many apps will require you to create mainGrid = (GridLayout) findViewById( getChildAt(5); Now all the items are declared, and now we have to make sure that they  Questions: I’m trying to build a basic tic-tac-toe game. Once the game is over I’m calling the gridlayout so that i can clear all the previous images. public void playAgain(View view){ gameActive = true; LinearLayout layout = (LinearLayout)findViewById(; layout.setVisibility(View.INVISIBLE); activePlayer = 0; for(int i = 0; i < gameState.length; i++){ gameState[i] = 2

replace your line by this

androidx.gridlayout.widget.GridLayout gridLayout = findViewById(;

android.widget.GridLayout.addView java code examples, setImageResource(R.drawable.ic_launcher); GridLayout. childTwo); GridLayout parent = (GridLayout) findViewById( Metrics[] metricsToAdd) { //​keeps track of the column we are adding to int col = -1; //start col at -1 so it becomes 0 getApp(actvname); if (!appAlreadyHere(app)) { ViewGroup item = mMainActivity. I am trying to call findviewbyid for GridLayout but where ever I place it returns null. I called it onCreate() method after setting contentView and inflating ViewStub. All children from content_pytanie1.xml returns properly except mQuestionLayout .

Either cast it like A.El Saeed mentioned in his answer

gridLayout = (GridLayout)findViewById(;

or post your XML code also because sometime, it may possible that there will be some typo in your XML code or maybe you are using different grid layout id than defined in the XML

Iterate through GridLayout childs to hide ImageViews of 3x3 board , Are you using support version in xml and you using non support version in code? GridLayout gridLayout = (GridLayout) findViewById(;. They are  The following are top voted examples for showing how to use android.widget.GridLayout.These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.

Change it


gridLayout = findViewById(;


gridLayout = (GridLayout) findViewById(;

You should cast the result from findViewById function

Android For Beginners. Developing Apps Using Android Studio, So when I am clicking button, the apps crashes. i = 0; i<gameState.length;i++){ gameState[i] = 2; } GridLayout gridLayout = (GridLayout) findViewById( You are using GridLayout from AndroidX package in your layout.xml and you are importing Gridlayout from package android.widget in your source code. Use one or the other and make them consistent.

Getting started with Android development - Tutorial, onCreate(savedInstanceState); setContentView(R.layout.full_image); ImageView imageView=(ImageView) findViewById(; imageView. GridLayout The difference between GridView and GridLayout In theGridView, all. The following are Jave code examples for showing how to use addView() of the android.widget.GridLayout class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.

Creación de juegos y aplicaciones para Android, The following layout file defines a layout using GridLayout . findViewById(​mytext); // TODO do something with the TextView } If you run your application and press the button your application crashes because you still  The following are Jave code examples for showing how to use getChildCount() of the android.widget.GridLayout class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.

El contraste entre los colores brillantes de nuestra app y el blanco sobre negro del GridLayout tablero = (GridLayout)findViewById(; casillas = new​  Best Java code snippets using android.widget.GridLayout (Showing top 20 results out of 315) GridLayout gridLayout = (GridLayout)findViewById(;