Android Build a Simple Android App with Java Basic Android Programming Making a Button Do Something

Michał Juszczuk
Michał Juszczuk
885 Points

Why, in my program the proper form of initialising factTextView is R.id.facttextView instead of R.id.factTextView?

I made everything as it was in toutorial, but Android Studio gave me a tip to write : factTextView = findViewById(R.id.facttextView); - It's working.

factTextView = findViewById(R.id.factTextView); - It's how it's made in toutorial, and it's not working in my case.

Why i had to write it differently, even though variable name is "factTextView" (capital T)?

This is the code: //declare our view variables private TextView factTextView; private Button showFactButton;

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

    // Assign Views from the layout file to the corresponding variables

    factTextView = findViewById(R.id.facttextView);
   showFactButton =findViewById(R.id.showFactbutton);

3 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,426 Points

You most likely used the name facttextView (lower case t) in your layout. Check your res/layout/activity_main.xml or similar file in that folder.

Michał Juszczuk
Michał Juszczuk
885 Points

Ok, dzięki za odpowiedź.

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

     // Assign the Views form the layout file to the corresponding variables
    factTextView = findViewById(R.id.factTextView);
    showFactButton = findViewById(R.id.showFactButton);