skip to Main Content
  • I created a new project with empty activity selected.
  • Then I added a new layout file called layout_extra.xml with id of layout_extra and it just contains one switch with id of switch1.
  • The activity_main.xml contains a button with id of button to navigate to the layout_extra.
  • The content of MainActivity.java is as follows:
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.button).setOnClickListener(v->{
            setContentView(R.layout.layout_extra);
        });

        findViewById(R.id.switch1).setOnClickListener(v->{
            findViewById(R.layout.layout_extra).setBackgroundColor(Color.RED);
        });
    }
}

Question

I got the following error on the Android Studio.

enter image description here

The tooltip gives the following info.

enter image description here

I did all the given suggestions but none works (the application crashes).

How to properly call resource id from within an anonymous class method?

3

Answers


  1. The setOnClickListener() use a new view so findViewById(R.layout.layout_extra).setBackgroundColor(Color.RED); didn’t work, you should try :

    public class MainActivity extends AppCompatActivity {
    
        private Layout layout_extra; // TextView, Button ...
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            findViewById(R.id.button).setOnClickListener(v->{
                setContentView(R.layout.layout_extra);
            });
    
            layout_extra = findViewById(R.layout.layout_extra);
    
            findViewById(R.id.switch1).setOnClickListener(v->{
                layout_extra.setBackgroundColor(Color.RED);
            });
        }
    }
    
    Login or Signup to reply.
  2. Hum, the method "findViewById()" is asking the ID of the Object not the Resource.

    Basically what i believe you should do is set the "ID" in the "XML" of your main layout, than you can use the method "findViewById()", any object has an id in "XML" even the layout can be set an ID.

    Ex:
    Set in the XML the id of you main object:

    android:id="@+id/layout_extra"

    in the code:

    findViewById(R.id.layout_extra).setBackgroundColor(Color.RED);

    Login or Signup to reply.
  3. I think I have got the error. You are calling setOnClickListener() on a null object. Hence, the updated code should be:

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // The following does not set the layout 'layout_extra' as root view until the button press
            findViewById(R.id.button).setOnClickListener(v->{
                // This block executes on button press
                setContentView(R.layout.layout_extra);
                final View layout_extra = findViewById(R.id.layout_extra);
                
                findViewById(R.id.switch1).setOnClickListener(v2->{
                    layout_extra.setBackgroundColor(Color.RED);
                });
            });
        }
    }
    

    where R.id.layout_extra refers to the id of the root element of layout_extra.xml.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search