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 (2014) Basic Android Programming Adding the onClick() Method

James N
James N
17,864 Points

i am getting errors when i run the program

a im getting the following errors when i attempt to run the program: error: cannot find symbol class TextView error: cannot find symbol class TextView error: cannot find symbol class Button error: cannot find symbol class Button error: package View does not exist error: package View does not exist Execution failed for task ':app:compileDebugJava'.

Compilation failed; see the compiler error output for details. my code for "FunFactsActivity.java" is:

package james.funfacts;

import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem;

public class FunFactsActivity extends Activity {

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

    // delcare our view variables and assign the views from the layout file
    final TextView factLabel = (TextView) findViewById(R.id.factTextView);
    Button showFactButton = (Button) findViewById(R.id.showFactButton);
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // the button was tapped, so change the fact label with a new fact
            String fact = "Ostriches can run faster than horses.";
            factLabel.setText(fact);
        }
    };
    showFactButton.setOnClickListener(listener);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.fun_facts, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

2 Answers

Stone Preston
Stone Preston
42,016 Points

the Button, TextView, and View classes have not been imported which is why you are getting those errors.

whenever you type a class name such as Button or TextView when creating a variable you need to be sure and select the option from autocomplete. If you dont use autocomplete, the classes do not get imported automatically. When you select the autocomplete value, Android Studio automatically imports them for you.

you can manually import them yourself by adding the following to your import statements at the top of the activity file

package james.funfacts;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
//add the import statements here to import the Button, TextView and View classes
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
James N
James N
17,864 Points

now i am getting only the last 3 errors.

Stone Preston
Stone Preston
42,016 Points

woops sorry didnt see the error about view. Edited my answer. simply import the View class yourself. you can add it after the other imports

import android.widget.Button;
import android.widget.TextView;
import android.view.View;
James N
James N
17,864 Points

thank you, my program works now!

Stone Preston
Stone Preston
42,016 Points

to prevent this kind of error from happening again just be sure and use autocomplete when typing class names that have not yet been imported.

James N
James N
17,864 Points

for some reason, i do not think i have auto complete.

Stone Preston
Stone Preston
42,016 Points

go to preferences -> editor -> code completion and make sure auto pop up code completion is checked

James N
James N
17,864 Points

it is already checked. but i still do not have autocomplete. EDIT: never mind i have autocomplete. it turns out i had powersaving mode on by mistake.

Thanks this helped me out as well!

Stone Preston
Stone Preston
42,016 Points

cool glad it helped you out