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
Eric Dotson
1,933 PointsR.Java causing problems with the crystal ball app
this code public static final int TextView1 = 0; is deleted evertime I try to run the crystal ball app by r.java which gives me a error message I use quick fix to replace it but as i try to run program the same thing keeps happening over and over again
7 Answers
Ben Jakuben
Treehouse TeacherHi Eric!
Yes, the R.java file is not intended to be edited. It gets generated by Eclipse each time you run the project. You should never have a reason to edit anything in there (though I made the same mistake as a beginner, too.) What exactly are you trying to accomplish?
Eric Dotson
1,933 Pointsfinal TextView answerLabel = (TextView) findViewById(R.id.TextView1); TextView1 is giving me a error TextView1 can not be resolved or is not a field quick fix gives me 2 fixes ok both changes the r.java. but the r.javachanges itself back so what do I do
Ben Jakuben
Treehouse TeacherIt should be a lowercase "t" for the ID (R.id.textView1):
final TextView answerLabel = (TextView) findViewById(R.id.textView1);
Eric Dotson
1,933 PointsI tried that but it did not work I still getting the same error
Ben Jakuben
Treehouse TeacherCan you paste in the code that you tried? You can paste in your whole MainActivity.java file. I copied my line of code from my real project, but perhaps something is different about yours.
Eric Dotson
1,933 Pointspackage com.example.ed;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
/* (non-Javadoc)
* @see android.support.v7.app.ActionBarActivity#onCreate(android.os.Bundle)
*/
@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);
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
//The button was clicked, so update the answer label with an answer
String answer = "Yes";
answerLabel.setText(answer);
}
};
getAnswerButton.setOnClickListener(onClickListener);
}
@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);
}
}
Ben Jakuben
Treehouse TeacherLooks good...perhaps something is wrong in your layout file? Next can you paste in activity_main.xml?
Eric Dotson
1,933 Points<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ed.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="118dp"
android:textSize="32sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="155dp"
android:text="Tell me" />
</RelativeLayout>
Ben Jakuben
Treehouse TeacherOkay, you're missing the TextView. :) You have two buttons here. Perhaps you dragged in the wrong one or something. Change the first Button to the following:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="118dp"
android:textSize="32sp" />
This changes the ID to textView1. You'll then to change where that ID is referenced in the second button. Change android:layout_alignTop="@+id/button1" to android:layout_alignTop="@+id/textView1".
Even with that done, your positioning looks a little off. I'm not sure how these will line up. If you'd like, you can replace the whole layout with this version from the project:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="32sp" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="Enlighten me!" />
</RelativeLayout>
The layout file is used to define how the screen will look. Each thing like a TextView or a Button is called a View, and to access those Views in code, we need to link to them with the correct ID and View type. The line that was failing for you:
final TextView answerLabel = (TextView) findViewById(R.id.textView1);
is looking for a TextView in the layout with an ID of textView1. Because you didn't have a TextView (nor was that ID being used), this line was failing.
Rondale Williams
1,892 PointsI'm having the same issues.
Ben Jakuben
Treehouse TeacherCheck out my latest answer above and see if it helps. :)
Eric Dotson
1,933 PointsIt's like you fix one problem and then 5 more show up!