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
Justin Ezor
2,193 PointsAndroid--Crystal Ball App image issue
There is a known error in the video for this project: http://teamtreehouse.com/library/build-a-simple-android-app/pretty-little-things/adding-an-image-2
Can someone please help me import the project files correctly? Upon trying this I completely messed up all my code and have 8 errors now saying: "The method onOptionsItemSelected(MenuItem) is undefined for the type ActionBarActivity" or something similar
I'm also getting this error "activity_main cannot be resolved or is not a field"
Here is my MainActivity.java file if that helps
package com.example.crystalball2;
import android.R;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private CrystalBall mCrystalBall = new CrystalBall();
// Member Variables are properties of an object
protected static final TextView answerLabel = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare our View variables and assign them the Views from the layout file
final TextView answerLabel = (TextView) findViewById(R.id.textView1);
Button getAnswerButton = (Button) findViewById(R.id.button1);
getAnswerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String answer = mCrystalBall.getAnAnswer();
// Update the label with our dynamic answer
answerLabel.setText(answer);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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
J Andrew Scott
30,626 PointsThat last error ( activity_main cannot be resolved or is not a field ) indicates that your R.java file isn't being regenerated in your project's gen folder. Have you made sure that v7 AppCompat is properly referenced as a library in your project setup and that the library project is also open in Eclipse?
Justin Ezor
2,193 PointsI am also getting this error: "R cannot be resolved to a variable"
Justin Ezor
2,193 PointsJustin Ezor
2,193 PointsThanks for the response J! Unfortunately, I'm so new that I'm not quite sure how to check that v7 AppCompat is properly referenced as a library or how to make sure that the library project is open in Eclipse. Can you explain how to do that please?
J Andrew Scott
30,626 PointsJ Andrew Scott
30,626 PointsIn Eclipse with your code window open, go to Project > Properties > Android and make sure you have the support library selected in the lower half of the form. The library's project name should be something similar to "android-support-v7-appcompat"