Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (retired 2014) Getting Started with Android Accessing Views in Code

Adama Sy
Adama Sy
7,076 Points

Can you see the syntax error and how to fix it? thanks

package com.example.crystal.ball;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

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

        // Declare our view variables
        TextView answerLabel = (TextView) findViewById(R.id.textView1);     
        Button getAnswerButton= (Button) findButtonById(R.id.Button);
    }

8 Answers

Oh , vaya error !

//Tu Error - Button getAnswerButton= (Button) findButtonById(R.id.Button);

//Respuesta - Button getAnswerButton= (Button)findViewById(R.id.Button);

  • Ademas , ten en cuenta de que un "id" no tiene que empezar con "MAYUSCULAS" - "R.id.Button" esta mal , lo mas seguro es que sea asi "R.id.button"

Saludos

Adama Sy
Adama Sy
7,076 Points

¡Hola Hice lo que me dijiste. Y este es el error que se incluye. ¿Y ahora qué? . / com / ejemplo / de cristal / de bola / MainActivity.java: 18: cannot find symbol símbolo: Botón variables ubicación: Clase com.example.crystal.ball.R.id          Botón getAnswerButton = (Button) findViewById (R.id.Button);                                                               ^ 1 error

Hola .

Es que no te diste cuenta de tu error aun , te lo explico mejor . un "ID" creado en la vista "xml" nunca debe tener un caracter en "MAYUSCULA" , en el pedazo de código que me enviaste figura lo siguiente

  • Button getAnswerButton = (Button) findViewById (R.id.Button) , "Button" esta en Mayúscula , debe ser "button" .
  • De todas formas te envio una imagen con el code challenge para que puedas corroborarlo.

Saludos Adama !

alt text

alt text

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Two things jump out:

1) A closing curly brace is missing at the bottom of the code you pasted. That could just be a copy/paste error in here, but wanted to point it out.

2) You are using the ID R.id.Button, but the ID you are supposed to use is R.id.button1. Note the lowercase "b" (case matters!) and the "1" at the end.

Hope this helps!

Adama Sy
Adama Sy
7,076 Points

I made it all in lower case. its not working still checked all the curly brace, restarted all by scratch I get this

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

    // Declare our view variables
    TextView answerLabel = (TextView) findViewById(R.id.textView1);     
    Button getAnswerButton = (Button) findViewbyId(R.id.button1);
}

}

and when I check the error this is what I get:

./com/example/crystal/ball/MainActivity.java:18: cannot find symbol symbol : method findViewbyId(int) location: class com.example.crystal.ball.MainActivity Button getAnswerButton = (Button) findViewbyId(R.id.button1); ^ 1 error

But this is what some student wrote and passed the challenge for me it's just not working

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Can you paste in all the code you are trying? It looks like there is some small typographical error somewhere causing these compiler errors, but we'll need all your code to troubleshoot it.

R.id.textView1?

Adama Sy
Adama Sy
7,076 Points

that is not wrong, it's what the first course says, that is what the teacher wrote, now I' looking at writing the button code. it says I have one error. Java is so weird and harder to understand.

did you run debugger and find which line your are getting the error?

Adama Sy
Adama Sy
7,076 Points

Error is on the last line yes. But I don't know what they mean when I read it. Honestly I find Android course not well explained. we go from part A to X then back to E in a very short period of time. Could be explained in a better way.

Maybe if you show here what the error says and the line its on maybe people can help.

Adama Sy
Adama Sy
7,076 Points

./com/example/crystal/ball/MainActivity.java:18: ';' expected Button getAnswerButton= (Button) findViewById(R.id.button1) ^ 1 error