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 trialJames N
17,864 Pointsi 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
42,016 Pointsthe 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;
Oleg Yanchinskiy
4,567 PointsThanks this helped me out as well!
Stone Preston
42,016 Pointscool glad it helped you out
James N
17,864 PointsJames N
17,864 Pointsnow i am getting only the last 3 errors.
Stone Preston
42,016 PointsStone Preston
42,016 Pointswoops sorry didnt see the error about view. Edited my answer. simply import the View class yourself. you can add it after the other imports
James N
17,864 PointsJames N
17,864 Pointsthank you, my program works now!
Stone Preston
42,016 PointsStone Preston
42,016 Pointsto 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
17,864 PointsJames N
17,864 Pointsfor some reason, i do not think i have auto complete.
Stone Preston
42,016 PointsStone Preston
42,016 Pointsgo to preferences -> editor -> code completion and make sure auto pop up code completion is checked
James N
17,864 PointsJames N
17,864 Pointsit 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.